我有一个Grails 3.3.6应用程序。我想从另一个Spring Boot应用程序中合并此应用程序中的一些neo4j功能。所以我从Spring Boot应用程序添加了以下部门-
compile "org.neo4j:neo4j:3.1.0"
compile "org.neo4j.driver:neo4j-java-driver:1.1.0"
compile "org.neo4j:neo4j-ogm-core:2.1.6"
compile "org.neo4j:neo4j-ogm-http-driver:2.1.6"
runtime "org.neo4j:neo4j-ogm-embedded-driver:2.1.6"
runtime "org.neo4j:neo4j-ogm-bolt-driver:2.1.6"
compile "org.springframework.boot:spring-boot-starter-data-neo4j"
compile 'org.springframework.data:spring-data-neo4j:4.2.0.RELEASE'
并将源文件放在我的Grails应用程序的src
文件夹中。 Spring Boot应用程序正在使用Configuration类来创建bean,它看起来像这样-
Configuration
@EnableNeo4jRepositories("com.server.repositories")
public class TestConfiguration {
@Value("${spring.data.neo4j.uri}")
String neo4jURL;
@Value("${spring.data.neo4j.username}")
String neo4jUser;
@Value("${spring.data.neo4j.password}")
String neo4jPass;
@Bean
public org.neo4j.ogm.config.Configuration configuration() {
return new org.neo4j.ogm.config.Configuration.Builder().credentials(neo4jUser, neo4jPass).uri(neo4jURL).build();
}
@Bean
public SessionFactory neo4jSessionFactory() {
// with domain entity base package(s)
return new SessionFactory(configuration(), "com.server.model");
}
@Bean
public Neo4jTransactionManager neo4jTransactionManager() {
return new Neo4jTransactionManager(neo4jSessionFactory());
}
@Bean
public Session neo4jSession() {
return neo4jTransactionManager().getSessionFactory().openSession();
}
运行grails应用程序时,出现Spring bean冲突异常。看起来Grails和neo4j试图创建一些具有相同名称的bean-
Unsatisfied dependency expressed through field 'neo4jSession'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.data.neo4j.transaction.SharedSessionCreator#0': Unsatisfied dependency expressed through method 'createSharedSession' parameter 0: Could not convert argument value of type [org.hibernate.internal.SessionFactoryImpl] to required type [org.neo4j.ogm.session.SessionFactory]: Failed to convert value of type 'org.hibernate.internal.SessionFactoryImpl' to required type 'org.neo4j.ogm.session.SessionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.hibernate.internal.SessionFactoryImpl' to required type 'org.neo4j.ogm.session.SessionFactory': no matching editors or conversion strategy found
我应该如何解决这些bean冲突?
答案 0 :(得分:1)
您必须指定Neo4j会话工厂的bean名称。 在您的情况下,这应该可以按预期工作:
@EnableNeo4jRepositories(sessionFactoryRef = "neo4jSessionFactory", basePackages = "com.server.repositories")
背景信息:Spring Data Neo4j将寻找一个名为sessionFactory
的bean。正如您在异常中看到的那样,Hibernate还会创建一个具有这种名称的bean,并且它们会发生冲突。