Spring Boot ConflictingBeanDefinitionException: Annotation-specified bean name for @Controller class

后端 未结 6 1935
南旧
南旧 2021-02-18 13:50

I keep getting the ConflictingBeanDefinitionException error in my Spring boot application. I am not entirely sure as to how to address it, I have several @Con

相关标签:
6条回答
  • 2021-02-18 14:37

    I had the same problem on a Spring integration test when I ran it with InteliJ.

    After a refactor, one of my controller class was actually duplicate in the /out/production/classes directory which is the default output directory for Intelij since version 2017.2. Since the gradle output directory is different (It's build/classes), the gradle clean goal had no effect.

    For me the solution was to manually remove /out/production/classes and re run my integration test.

    For a possible durable solution not having 2 output directories see here

    0 讨论(0)
  • 2021-02-18 14:40

    I was having the same problem with a generated .war file from spring-boot. the approved solution (Timothy Tuti's own solution) didn't quite work for me exactly as-is, but I tweaked it a little bit and it worked. I just added the following line to my Application.java:

    @ComponentScan(basePackages = { "com.mypackage" })

    For reference, here goes my full Application.java

    package com.inmoment.devchallenge;
    import org.neo4j.graphdb.GraphDatabaseService;
    import org.neo4j.graphdb.factory.GraphDatabaseFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.context.web.SpringBootServletInitializer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
    import org.springframework.data.neo4j.config.Neo4jConfiguration;
    
    @SpringBootApplication
    @Configuration
    @ComponentScan(basePackages = { "com.inmoment.devchallenge.controller" })
    @EnableAutoConfiguration
    public class Application extends SpringBootServletInitializer {
    
        @Configuration
        @EnableNeo4jRepositories(basePackages = "com.inmoment.devchallenge.repository")
        static class ApplicationConfig extends Neo4jConfiguration {
    
            public ApplicationConfig() {
                setBasePackage("com.inmoment.devchallenge.repository");
            }
    
            @Bean
            GraphDatabaseService graphDatabaseService() {
                return new GraphDatabaseFactory().newEmbeddedDatabase("accessingdataneo4j.db");
            }
        }
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    
    }
    
    0 讨论(0)
  • 2021-02-18 14:42

    I ran into the same problem but for a different reason.

    This can also occur if you move your classes around in your project and fail to do a 'clean'.

    I use gradle with spring-boot plugin. Now I usually run:

    $> ./gradlew clean bootRun
    
    0 讨论(0)
  • 2021-02-18 14:44

    The solution, as I found out, is to disable double initialization by including a filter in the component scan. In my case:

    @EnableScheduling
    @EnableAspectJAutoProxy
    @EnableCaching
    @Configuration
    @ComponentScan(basePackages = { "org.kemri.wellcome.hie" }, 
        excludeFilters = {@Filter(value = Controller.class, type = FilterType.ANNOTATION)})
    @EnableAutoConfiguration
    @PropertySource("classpath:application.properties")
    public class Application extends SpringBootServletInitializer {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    0 讨论(0)
  • 2021-02-18 14:46

    I encountered this with mvn after changing several folder names and related package names. Than I applied maven clean and run spring boot again, all solved:

    mvn clean
    mvn spring-boot:run
    
    0 讨论(0)
  • 2021-02-18 14:49

    It seems you have two entityManagerFactory, one you will autowire and one you resolve programmatically as Bean:

    @Autowired
    private LocalContainerEntityManagerFactoryBean entityManagerFactory;
    
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    ...
    }
    

    I think you just need your configured Factory in entityManagerFactory() method.

    0 讨论(0)
提交回复
热议问题