java - Spring Data Neo4j 4.x 和 sdn-大学 : Neo4jTemplate no autowire'ing

标签 java spring spring-data-neo4j spring-data-neo4j-4

我正在将我们的应用程序(该应用程序以 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/

相关文章:

java - Joda-Time : convert SimpleDateFormat pattern to DateTimeFormat. forPattern

java - App Engine 聊天应用程序

java - mod_deflate 与 IBM WebSphere 和 IBM IHS

java - Spring Boot JPA 应用程序无法从 Spring Boot 2.0.2 启动,但可以在 Spring Boot 1.5.13 中正常运行

java - 如何在 Spring Boot 中将 application.properties 文件外部化为例如外部依赖 JAR?

neo4j - 我们应该使用特定于节点的关系还是节点不可知的关系?

java - 获取联系方式失败?

java - @Query 用于连接两个表

neo4j - 如何将 spring-data-neo4j 与空间索引和 cypher 一起使用?

java - Spring-data-neo4j + @Query 抛出 PropertyReferenceException