所以我有一个使用spring-data-neo4j的项目,遇到了一个不起眼的问题。我使用java配置spring-neo4j,Neo4jConfig.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和一个实现,Bean DaoImpl.java:
@Repository
public class BeanDaoImpl implements BeanDao {
public String getStr() {
return "from BeanImpl";
}
}
然后我有一个服务使用DaoImpl,注意自动装配的是BeanDaoImpl,而不是BeanDao:
@Service
public class MyBeanService {
@Autowired
private BeanDaoImpl daoImpl;
public String getServiceString(){
return daoImpl.getStr();
}
}
这是我的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个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
可能性是我删除Neo4jConfig
或使用@Autowired BeanDao
测试可以通过。另外,我使用一个常见的@Configuration
类,测试仍然通过,所以问题可能在Neo4jConfiguration
,有人可以告诉我为什么以及如何解决这个问题?在实际项目中,我无权将@Autowired BeanDaoImpl
更改为@Autowired BeanDao
。