带有@ EnableExperimentalNeo4jRepositories注释和SpringBoot 1.4.2的NoUniqueBeanDefinitionException

时间:2016-11-28 17:17:38

标签: spring-boot spring-data-neo4j-4 neo4j-ogm

我遇到了Spring boot 1.4.2.M1和@ EnableExperimentalNeo4jRepositories的问题。

这似乎是两个bean之间的冲突,一个弹簧启动,一个spring-data-neo4j。

这是一个堆栈跟踪摘录:

18:12:15.891 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.neo4j.ogm.session.Session' available: expected single matching bean but found 2: getSession,org.springframework.data.neo4j.transaction.SharedSessionCreator#0

另一个......

Parameter 0 of method setSession in org.springframework.data.neo4j.repository.support.Neo4jRepositoryFactoryBean required a single bean, but 2 were found:
    - getSession: defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration$SpringBootNeo4jConfiguration.class]
    - org.springframework.data.neo4j.transaction.SharedSessionCreator#0: defined by method 'createSharedSession' in null

任何人都知道如何解决这个问题?

以下是我的Neo4j配置

package com.domain.core.context;

import javax.annotation.PostConstruct;

import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.neo4j.ogm.session.event.Event;
import org.neo4j.ogm.session.event.EventListenerAdapter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableExperimentalNeo4jRepositories;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@ComponentScan("com.domain")
@EnableExperimentalNeo4jRepositories(basePackages = "com.domain.core.repository")
@EnableTransactionManagement
@SpringBootApplication(exclude = Neo4jDataAutoConfiguration.class)
public class TestPersistenceContext {

    @PostConstruct
    public void init() {
        log.info("TheScene.Co: Initializing Test Neo4jConfig ...");
    }

    @Bean
    public Neo4jTransactionManager transactionManager() throws Exception { 
        return new Neo4jTransactionManager(sessionFactory());
    }

    @Bean
    public SessionFactory sessionFactory() {
        return new SessionFactory(getConfiguration(), "com.domain") {

            @Override
            public Session openSession() {
                Session session = super.openSession();
                session.register(new EventListenerAdapter() {
                    @Override
                    public void onPreSave(Event event) {
                        // do something - like set an id on an object
                        log.debug("***** Saving domain object ********");
                    }
                });

                return session;
            }
        };
    }


    @Bean
    public org.neo4j.ogm.config.Configuration getConfiguration() {
        org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
        config.driverConfiguration().setCredentials("neo4j", "password")
                .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver");

        return config;
    }

}

1 个答案:

答案 0 :(得分:1)

您必须使用Spring Data Neo4j(SDN)版本4.2.0.M1。这个里程碑版本被发布以获得4.1.x的几个重大变化的反馈。

SDN 4.2.0.RC1应在本周晚些时候发布,但目前4.2.0.BUILD-SNAPSHOT在Decemeber的Spring Data Ingalls发布列车中实际上非常稳定。

来自SDN 4.0 / 4.1的用户

I have written a guide,了解如何升级到快照构建。

在本指南there is a link中的示例项目分支中,该分支显示了如何使用Spring Boot 1.4.x使用此版本进行一些小的工作。

即将发布的Spring Boot 1.5,我们已经更新了所有自动配置,以便直接使用SDN 4.2开箱即用。我们将更新Spring Boot的文档更接近发布。