我正在将我们的应用程序(该应用程序以 sdn-university
为模型)从 Spring Data Neo4j 3.x 升级到 Spring Data Neo4j 4.0.0.BUILD-SNAPSHOT。添加 Neo4jTemplate 的 Autowiring 实例时,启动时抛出以下异常:
... Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.neo4j.template.Neo4jTemplate school.service.UserServiceImpl.template; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.neo4j.template.Neo4jTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 21 more ...
为了验证结果,我采用了最新的 sdn-university
——它现在也使用了 SDN4.x 4.0.0.BUILD-SNAPSHOT ——来自 https://github.com/neo4j-examples/sdn4-university并修改了 StudentServiceImpl 以包含 Neo4jTemplate 的 Autowiring 实例。发出 mvn clean spring-boot:run -U
后,会重现错误(如我们的应用程序中所示)。
需要对 sdn-university
进行哪些更改才能成功使用 Neo4jTemplate?
最佳答案
这在 SDN4 中不起作用的原因是 Neo4jTemplate
及其接口(interface) Neo4jOperations
均使用 @Repository
进行注释。因此,Spring 创建了 Neo4jTemplate
类的代理,但由于类型不匹配,该类无法正确 Autowiring 。
最好的方法是针对 Neo4jOperations
接口(interface)而不是 Neo4jTemplate
类进行编码。尽管我个人总是倾向于针对接口(interface)进行编码,但 SDN4 最终版本中是否会出现这种情况尚未确定。
您可能还需要声明 Neo4jOperations
作为 bean 工厂方法的返回类型:
@Bean
public Neo4jOperations neo4jTemplate() throws Exception {
return new Neo4jTemplate(getSession());
}
关于java - Spring Data Neo4j 4.x 和 sdn-大学 : Neo4jTemplate no autowire'ing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837949/