我正在尝试使用基于java的配置配置Spring-Data-Neo4j
示例。当我试图运行测试用例时,下面的堆栈跟踪打印。根据例外的是,对于事务Neo4jTemplate获取多个事务管理器,正如我所想。
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: neo4jTransactionManager,jtaTransactionManagerFactoryBean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:298)
at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:354)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:256)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy68.findBySchemaPropertyValue(Unknown Source)
at com.harmeetsingh13.repositorytest.NodePersistenceTest.getObjectFromRepo(NodePersistenceTest.java:49)
我的基于Neo4j Java的配置:
@Configuration
@EnableTransactionManagement
@PropertySource(value="classpath:properties/db.properties")
@ComponentScan(basePackages = "com.harmeetsingh13.entities")
@EnableNeo4jRepositories(basePackages = "com.harmeetsingh13.repository")
public class Neo4jConfig extends Neo4jAspectConfiguration{
@Resource
private Environment env;
public Neo4jConfig() {
setBasePackage("com.harmeetsingh13.entities");
}
@Bean(name = "graphDatabaseService")
public GraphDatabaseService getGraphDatabaseService(){
GraphDatabaseFactory databaseService = new GraphDatabaseFactory();
return databaseService.newEmbeddedDatabase(env.getProperty("db.store.directory"));
}
@Autowired
@Bean(name = "jtaTransactionManagerFactoryBean")
public JtaTransactionManagerFactoryBean neo4jTransactionManagerFactoryBean
(GraphDatabaseService graphDatabaseService) throws Exception {
JtaTransactionManagerFactoryBean factoryBean =
new JtaTransactionManagerFactoryBean(graphDatabaseService);
return factoryBean;
}
@Autowired
@Bean(name= {"neo4jTransactionManager"})
public PlatformTransactionManager getTransactionManager
(JtaTransactionManagerFactoryBean jtaTransactionManagerFactoryBean) throws Exception {
ChainedTransactionManager transactionManager = new ChainedTransactionManager(jtaTransactionManagerFactoryBean.getObject());
return transactionManager;
}
}
我的测试案例:
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional(value = "neo4jTransactionManager")
@ContextConfiguration(classes = Neo4jConfig.class, loader = AnnotationConfigContextLoader.class)
public class NodePersistenceTest {
@Autowired
private Neo4jTemplate neo4jTemplate;
@Autowired
private RepositoryMovie movieRepo;
@Test
@Transactional
public void persistMovie() {
Movie movie = new Movie();
movie.setId(13L);
movie.setTitle("Dark Knight");
Movie returnMovie = neo4jTemplate.save(movie);
assertThat(returnMovie, notNullValue());
}
@Test
public void getObjectFromRepo() {
Movie movie = movieRepo.findBySchemaPropertyValue("id", 13);
assertThat(movie, notNullValue());
}
}
在测试用例中,第一个测试用例将成功运行,但是在第二个测试用例中产生异常并且我们的测试用例失败。