Java spring-data-neo4j与自动连线冲突

Java spring-data-neo4j与自动连线冲突,java,spring,spring-mvc,spring-data-neo4j,Java,Spring,Spring Mvc,Spring Data Neo4j,所以我有一个使用spring-data-neo4j的项目,遇到了一个模糊的问题。 我对spring-neo4j、Neo4jConfig.java使用java配置: @Configuration @EnableNeo4jRepositories(basePackages = "org.neo4j.example.repository") @EnableTransactionManagement public class Neo4jConfig extends Neo4jConfiguration

所以我有一个使用spring-data-neo4j的项目,遇到了一个模糊的问题。 我对spring-neo4j、Neo4jConfig.java使用java配置:

@Configuration
@EnableNeo4jRepositories(basePackages = "org.neo4j.example.repository")
@EnableTransactionManagement
public class Neo4jConfig extends Neo4jConfiguration {

    @Bean
    public SessionFactory getSessionFactory() {
        // with domain entity base package(s)
        return new SessionFactory("org.neo4j.example.domain");
    }

    // needed for session in view in web-applications
    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }

}
我有一个DAO和一个实现BeanDaoImpl.java:

@Repository
public class BeanDaoImpl implements BeanDao {
    public String getStr() {
        return "from BeanImpl";
    }
}
然后我有一个使用DaoImpl的服务,请注意,自动连线是BeanDaoImpl,而不是BeanDao:

以下是我的app-context.xml:

    <context:component-scan base-package="com.springconflict" />
版本为springframework 4.2.5,spring-data-neo4j 4.1.11,似乎spring-data-neo4j与spring 4.2.5兼容

以下是编译错误信息:

原因:org.springframework.beans.factory.BeanCreationException:无法自动连接字段:private com.springconflict.dao.impl.BeanDaoImpl com.springconflict.service.MyBeanService.daoImpl;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[com.springconflict.dao.impl.BeanDaoImpl]的符合条件的bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowiredrequired=true}

我要么删除Neo4jConfig,要么使用@autowiredbean,这样测试就可以通过。另外,我使用了一个常见的@Configuration类,测试仍然通过,所以Neo4jConfiguration中可能存在问题,有人能告诉我为什么以及如何解决这个问题吗? 我无权在real project中将@Autowired BeanDao更改为@Autowired BeanDao


TL;DR Answer:在Neo4jConfig中定义以下附加bean,从而覆盖SDN 4.x的PersistenceExceptionTranslationPostProcessor的默认实例

详细回答:Spring数据Neo4j使用Springs PersistenceException TranslationPostProcessor将Neo4j OGM异常重新映射到Spring数据异常中。这些处理器应用于@Repository类型的所有组件

因此,后处理器现在在BeanDoaImpl周围形成一个代理,因此它基本上类似于另一个类。如果您使用接口,那么这很好,但是如果您想将bean分配给具体类,那么就不行了

Spring可以全局配置来创建子类,而不是基于标准Java接口的代理。它通过使用CGLIB来创建子类,但默认情况下这不是启用的,而且对于Spring 4.2.x和Spring数据Neo4j 4.2.x来说,无论如何都不重要,因为上面的后处理器独立于该机制

用一个明确配置的替换它可以解决您的问题

从架构的角度或干净的代码角度来看,最好是注入接口,而不是具体的类

如果我能帮助您,请告诉我:

    <context:component-scan base-package="com.springconflict" />
@Bean
PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
    PersistenceExceptionTranslationPostProcessor p = new PersistenceExceptionTranslationPostProcessor();
    p.setProxyTargetClass(true);
    return p;
}