SpringBoot2.x 集成Neo4j 提示Error creating bean with name ‘transactionManager‘ defined in class path ***

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transactionManager' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'transactionManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1178)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1072)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1016)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:344)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:339)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:384)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
	at com.sun.proxy.$Proxy106.getStoragePath(Unknown Source)

造成的原因是Neo4j 初始化sessionFactory 没有找到Neo4j 的事务管理器 transactionManager,查看SpringBoot 2.x 涉及Neo4j 自动注入配置对象:spring-boot-autoconfigure-x.x.x.release.jar的org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration.java。

SpringBoot2.x 集成Neo4j 提示Error creating bean with name ‘transactionManager‘ defined in class path ***_第1张图片

问题核心:事务接口PlatformTransactionManager实例化冲突

myBatis 事务管理器实例化对象:spring-jdbc-x.x.x.release.jar 
                   实例化对象:org.springframework.jdbc.datasource.DataSourceTransactionManager.java(注意:org.springframework.transaction.support.AbstractPlatformTransactionManager 实现PlatformTransactionManager 接口)

neo4j 事务管理器实例化对象:spring-data-neo4j-x.x.x.release.jar
                   实例化对象:org.springframework.data.neo4j.transaction.Neo4jTransactionManager (注意:org.springframework.transaction.support.AbstractPlatformTransactionManager 实现PlatformTransactionManager 接口)

现在的问题已经很明确 neo4j 与mybatis 事务冲突
解决办法是:放弃springboot 自动注入neo4j 的配置服务对象:org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration.java 

使用spring 自定义neo4j 配置对象,核心功能代码如下:
package com.zzg.ucas.config;

import org.neo4j.ogm.session.SessionFactory;
import org.neo4j.ogm.session.transaction.DefaultTransactionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;

@Configuration
@EnableNeo4jRepositories("com.zzg.ucas.neo4j.repository") // 声明neo4j repository存放地址
public class Neo4jConfig {
	@Bean
    public org.neo4j.ogm.config.Configuration getConfiguration() {
        org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
        config.driverConfiguration()
                .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
                .setURI("http://neo4j:[email protected]:7474");
        return config;
    }
	
	@Bean(name="sessionFactory")
    public SessionFactory getSessionFactory() {
        /**
         * 如果不指定节点映射的java bean路径,保存时会报如下警告,导致无法将节点插入Neo4j中
         * ... is not an instance of a persistable class
         */
        return new SessionFactory(getConfiguration(), "com.zzg.ucas.neo4j.ogm");
    }
	
	/**
	 * 解决MyBatis 事务与Neo4j 事务冲突
	 * @return
	 */
	@Bean
	public DefaultTransactionManager getDefaultTransactionManager(){
		return new DefaultTransactionManager(getSessionFactory().openSession());
	}
}

 

你可能感兴趣的:(neo4j)