chr*_*leu 5 spring spring-annotations spring-data-neo4j
neo4jDatabase()很好,但是environment在graphDatabaseService()...中为什么总是为空?
@Configuration
@PropertySource("classpath:/neo4j.properties")
@EnableNeo4jRepositories("reservation.repository.neo4j")
public class Neo4jConfig extends Neo4jConfiguration {
@Inject
Environment environment;
@Bean(initMethod = "setupDb")
public Neo4jDatabase neo4jDatabase() {
// Environment fine here...
return new Neo4jDatabase(this.environment.getProperty("data.file.path"));
}
@Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
if (environment == null) {
// Always gets here for some reason...why?
return new EmbeddedGraphDatabase("/Temp/neo4j/database");
} else {
return new EmbeddedGraphDatabase(this.environment.getProperty("database.path"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
版本:Spring 3.2.0.RELEASE,spring-data-neo4j 2.1.0.RELEASE。
如果其他人遇到同样的问题 - 以下对我有用:
@Configuration
@PropertySource("classpath:neo4j.properties")
@EnableNeo4jRepositories(basePackages = "com.mydomain.neo4j.repo")
public class Neo4jConfig
{
@Autowired
Environment environment;
@Bean(name="graphDatabaseService", destroyMethod = "shutdown")
public GraphDatabaseService getGraphDatabaseService()
{
// any custom graph db initialization
return new EmbeddedGraphDatabase(this.environment.getProperty("database.path"));
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我不会扩展 Neo4jConfiguration。它只是变成了 Autowired 依赖项的意大利面条,而环境成员变量在 graphDatabaseService 初始化需要时从未设置过。你可以使用 a 让它工作,@PostConstruct但最终会得到一堆NotInTransactionException' 。我没有时间深入研究原因 - 相反,在您的主 AppConfig 类中,您只需导入自定义 Neo4j 配置以及基本抽象 Neo4j 配置类。本质上,您正在代码中执行 XML 配置将执行的操作。
@Configuration
@Import({Neo4jConfig.class, Neo4jConfiguration.class})
@ComponentScan(basePackages = {"com.mydomain"}, excludeFilters = @Filter({Controller.class, Configuration.class}))
public class MainConfig
{
// any other configuration you have
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7920 次 |
| 最近记录: |