我在spring-boot应用程序中使用spring-data-neo4j。我通过继承Neo4jConfiguration类,按照spring.io guides和许多其他地方的建议进行了配置。这在数据库位置在提供的示例中进行硬编码时有效。但是,当我想使用占位符从属性文件中检索数据库位置时,它不会被检索,我得到null。这是代码
@Configuration
@EnableNeo4jRepositories(basePackageClasses = {MyRepository.class})
public class Neo4jConfig extends Neo4jConfiguration {
@Value("${neo4j.location}")
private String neo4jDatabaseLocation;
@Bean
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory()
.newEmbeddedDatabase(neo4jDatabaseLocation);
}
...
这通常适用于任何其他配置类,但不适用于此类,因为Neo4jConfiguration class有几个标有@Autowired的方法。这会导致循环引用,并且未正确初始化。这可以在日志中看到:
2014-09-06 20:59:45.168 DEBUG 4665 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [c.m.f.Neo4jConfig$$EnhancerBySpringCGLIB$$7165d752]: AutowiredFieldElement for private javax.validation.Validator org.springframework.data.neo4j.config.Neo4jConfiguration.validator
2014-09-06 20:59:45.169 DEBUG 4665 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [c.m.f.Neo4jConfig$$EnhancerBySpringCGLIB$$7165d752]: AutowiredMethodElement for public void org.springframework.data.neo4j.config.Neo4jConfiguration.setConversionService(org.springframework.core.convert.ConversionService)
2014-09-06 20:59:45.169 DEBUG 4665 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [c.m.f.Neo4jConfig$$EnhancerBySpringCGLIB$$7165d752]: AutowiredMethodElement for public void org.springframework.data.neo4j.config.Neo4jConfiguration.setGraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService)
2014-09-06 20:59:45.169 DEBUG 4665 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [c.m.f.Neo4jConfig$$EnhancerBySpringCGLIB$$7165d752]: AutowiredFieldElement for private java.lang.String c.m.f.Neo4jConfig.neo4jDatabaseLocation
2014-09-06 20:59:45.169 DEBUG 4665 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'c.m.f.Neo4jConfig' to allow for resolving potential circular references
2014-09-06 20:59:45.171 DEBUG 4665 --- [ main] o.s.b.f.annotation.InjectionMetadata : Processing injected method of bean 'c.m.f.Neo4jConfig': AutowiredFieldElement for private javax.validation.Validator org.springframework.data.neo4j.config.Neo4jConfiguration.validator
2014-09-06 20:59:45.182 DEBUG 4665 --- [ main] o.s.b.f.annotation.InjectionMetadata : Processing injected method of bean 'c.m.f.Neo4jConfig': AutowiredMethodElement for public void org.springframework.data.neo4j.config.Neo4jConfiguration.setConversionService(org.springframework.core.convert.ConversionService)
2014-09-06 20:59:45.183 DEBUG 4665 --- [ main] o.s.b.f.annotation.InjectionMetadata : Processing injected method of bean 'c.m.f.Neo4jConfig': AutowiredMethodElement for public void org.springframework.data.neo4j.config.Neo4jConfiguration.setGraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService)
2014-09-06 20:59:45.184 DEBUG 4665 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'graphDatabaseService'
2014-09-06 20:59:45.184 DEBUG 4665 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'graphDatabaseService'
2014-09-06 20:59:45.185 DEBUG 4665 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2014-09-06 20:59:45.185 DEBUG 4665 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2014-09-06 20:59:45.188 DEBUG 4665 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Returning eagerly cached instance of singleton bean 'c.m.f.Neo4jConfig' that is not fully initialized yet - a consequence of a circular reference
正如您所看到的,我在这里想要实现的是不对数据库位置进行硬编码。这个循环引用问题有没有解决方法?或者可能是其他任何配置方式?由于这是一个spring-boot应用程序,我没有任何Xml配置,如果有可能我想保持这种方式。
答案 0 :(得分:1)
您是否尝试过将neo4jDatabaseLocation作为参数传递:
@Configuration
@EnableNeo4jRepositories(basePackageClasses = {MyRepository.class})
public class Neo4jConfig extends Neo4jConfiguration {
@Bean
public GraphDatabaseService graphDatabaseService(@Value("${neo4j.location}") String neo4jDatabaseLocation) {
return new GraphDatabaseFactory()
.newEmbeddedDatabase(neo4jDatabaseLocation);
}
...
}
答案 1 :(得分:1)
建议的建议对我正在使用的spring框架版本不起作用。作为一种解决方法,我在另一个配置文件中定义了GraphDatabaseService
bean,该文件没有扩展Neo4jConfiguration。请注意,无论如何都需要此Neo4jConfig
类。