Spring Boot ConflictingBeanDefinitionException: The bean name specified for the annotation for the @Controller class

I keep getting the ConflictingBeanDefinitionException error in my Spring boot application. I'm not quite sure how to do this, I have some annotated @Configuration classes to help configure Thymeleaf, Spring Security, and the Web. Why is the application trying to configure homeController twice? (and where is he trying to do this?)

Mistake:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.kemri.wellcome.hie.Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'homeController' for bean class [org.kemri.wellcome.hie.HomeController] conflicts with existing, non-compatible bean definition of same name and class [org.kemri.wellcome.hie.controller.HomeController] 

My Spring initializer of the main loading boot:

 @EnableScheduling @EnableAspectJAutoProxy @EnableCaching @Configuration @ComponentScan @EnableAutoConfiguration public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) { return application.sources(Application.class); } } 

My database configuration file:

 @Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages="org.kemri.wellcome.hie.repositories") @PropertySource("classpath:application.properties") public class DatabaseConfig { @Autowired private Environment env; @Autowired private DataSource dataSource; @Autowired private LocalContainerEntityManagerFactoryBean entityManagerFactory; @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName")); dataSource.setUrl(env.getProperty("spring.datasource.url")); dataSource.setUsername(env.getProperty("spring.datasource.username")); dataSource.setPassword(env.getProperty("spring.datasource.password")); return dataSource; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); entityManagerFactory.setDataSource(dataSource); // Classpath scanning of @Component, @Service, etc annotated class entityManagerFactory.setPackagesToScan( env.getProperty("spring.jpa.hibernate.entitymanager.packagesToScan")); // Vendor adapter HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); entityManagerFactory.setJpaVendorAdapter(vendorAdapter); // Hibernate properties Properties additionalProperties = new Properties(); additionalProperties.put( "hibernate.dialect", env.getProperty("spring.jpa.hibernate.dialect")); additionalProperties.put( "hibernate.showsql", env.getProperty("spring.jpa.hibernate.showsql")); additionalProperties.put( "hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.hbm2ddl.auto")); entityManagerFactory.setJpaProperties(additionalProperties); return entityManagerFactory; } @Bean public JpaTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory( entityManagerFactory.getObject()); return transactionManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } } 

My Thymeleaf configuration file:

 @Configuration public class ThymeleafConfig { @Bean public ServletContextTemplateResolver templateResolver(){ ServletContextTemplateResolver thymeTemplateResolver = new ServletContextTemplateResolver(); thymeTemplateResolver.setPrefix("/WEB-INF/views/"); thymeTemplateResolver.setSuffix(".html"); thymeTemplateResolver.setTemplateMode("HTML5"); return thymeTemplateResolver; } @Bean public SpringSecurityDialect springSecurityDialect(){ SpringSecurityDialect dialect = new SpringSecurityDialect(); return dialect; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine engine = new SpringTemplateEngine(); engine.addTemplateResolver(templateResolver()); Set<IDialect> dialects = new HashSet<IDialect>(); dialects.add(springSecurityDialect()); engine.setAdditionalDialects(dialects); return engine; } @Bean public ThymeleafViewResolver thymeleafViewResolver() { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine()); resolver.setViewClass(ThymeleafTilesView.class); resolver.setCharacterEncoding("UTF-8"); return resolver; } 

}

My web config class:

 @Configuration @PropertySource("classpath:application.properties") public class WebConfig extends WebMvcAutoConfigurationAdapter { @Autowired private Environment env; @Bean public JavaMailSenderImpl javaMailSenderImpl() { JavaMailSenderImpl mailSenderImpl = new JavaMailSenderImpl(); mailSenderImpl.setHost(env.getProperty("smtp.host")); mailSenderImpl.setPort(env.getProperty("smtp.port", Integer.class)); mailSenderImpl.setProtocol(env.getProperty("smtp.protocol")); mailSenderImpl.setUsername(env.getProperty("smtp.username")); mailSenderImpl.setPassword(env.getProperty("smtp.password")); Properties javaMailProps = new Properties(); javaMailProps.put("mail.smtp.auth", true); javaMailProps.put("mail.smtp.starttls.enable", true); mailSenderImpl.setJavaMailProperties(javaMailProps); return mailSenderImpl; } @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager(); } } 

My controller (where there is a controller configuration error)

 @Controller public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { logger.info("Welcome home! The client locale is {}.", locale); Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); model.addAttribute("serverTime", formattedDate ); return "index.html"; } } 

What can cause a ConflictingBeanDefinitionException error for my controller class?

+15
java spring spring-boot spring-mvc spring-security
source share
6 answers

The solution, as I found out, is to disable dual initialization by including the 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); } } 
+7
source share

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

This can also happen if you are moving classes in your project and not doing clean.

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

 $> ./gradlew clean bootRun 
+31
source share

I had the same problem in the Spring Integration Test when I ran it with InteliJ .

After refactoring, one of my controller classes was actually duplicated 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 builds / classes), the goal of cleaning gradle had no effect.

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

For a possible long-term solution that does not have 2 output directories, see here

+7
source share

It seems you have two entityManagerFactory, one of which you will be autowire, and one of them programmatically called Bean:

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

It seems to me that you only need the factory-configured entityManagerFactory () method.

0
source share

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

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

For reference, here comes 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
source share

I came across this with mvn after changing several folder names and associated package names. After I applied maven clean and started the boot spring again, everything was solved:

 mvn clean mvn spring-boot:run 
0
source share

All Articles