title: Spring Boot ConflictingBeanDefinitionException: Annotation-specified bean name for @Controller class
body: <p>I keep getting the <code>ConflictingBeanDefinitionException</code> error in my Spring boot application. I am not entirely sure as to how to address it, I have several <code>@Configuration</code> annotated classes helping to set up Thymeleaf, Spring Security and Web. Why is the application trying to setup the <code>homeController</code> twice? (and where is it trying to do this?)</p>
  <p>The error is:</p>
  <pre><code>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]
  </code></pre>
  <p>My spring boot main application initializer:</p>
  <pre><code>@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);
      }
  
  }
  </code></pre>
  <p>My database config file:</p>
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages=&quot;org.kemri.wellcome.hie.repositories&quot;)
  @PropertySource(&quot;classpath:application.properties&quot;)
  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(&quot;spring.datasource.driverClassName&quot;));
      dataSource.setUrl(env.getProperty(&quot;spring.datasource.url&quot;));
      dataSource.setUsername(env.getProperty(&quot;spring.datasource.username&quot;));
      dataSource.setPassword(env.getProperty(&quot;spring.datasource.password&quot;));
      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(&quot;spring.jpa.hibernate.entitymanager.packagesToScan&quot;));
      
      // Vendor adapter
      HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
      
      // Hibernate properties
      Properties additionalProperties = new Properties();
      additionalProperties.put(
          &quot;hibernate.dialect&quot;, 
          env.getProperty(&quot;spring.jpa.hibernate.dialect&quot;));
      additionalProperties.put(
          &quot;hibernate.showsql&quot;, 
          env.getProperty(&quot;spring.jpa.hibernate.showsql&quot;));
      additionalProperties.put(
          &quot;hibernate.hbm2ddl.auto&quot;, 
          env.getProperty(&quot;spring.jpa.hibernate.hbm2ddl.auto&quot;));
      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();
    }
  
  }
  </code></pre>
  <p>My Thymeleaf config file:</p>
  <pre><code>@Configuration
  public class ThymeleafConfig {
  
  @Bean
  public ServletContextTemplateResolver templateResolver(){
      ServletContextTemplateResolver thymeTemplateResolver = new ServletContextTemplateResolver();
      thymeTemplateResolver.setPrefix(&quot;/WEB-INF/views/&quot;);
      thymeTemplateResolver.setSuffix(&quot;.html&quot;);
      thymeTemplateResolver.setTemplateMode(&quot;HTML5&quot;);
      return thymeTemplateResolver;
  }   
  
  @Bean
  public SpringSecurityDialect springSecurityDialect(){
      SpringSecurityDialect dialect = new SpringSecurityDialect();
      return dialect;
  }
  
  @Bean
  public SpringTemplateEngine templateEngine() {
      SpringTemplateEngine engine = new SpringTemplateEngine();   
      engine.addTemplateResolver(templateResolver());
      Set&lt;IDialect&gt; dialects = new HashSet&lt;IDialect&gt;();
      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(&quot;UTF-8&quot;);
      return resolver;
  }
  </code></pre>
  <p>}</p>
  <p>My Web config class:</p>
  <pre><code>@Configuration
  @PropertySource(&quot;classpath:application.properties&quot;)
  public class WebConfig extends WebMvcAutoConfigurationAdapter  {
      
      @Autowired
      private Environment env;
      
      @Bean
      public JavaMailSenderImpl javaMailSenderImpl() {
          JavaMailSenderImpl mailSenderImpl = new JavaMailSenderImpl();
          mailSenderImpl.setHost(env.getProperty(&quot;smtp.host&quot;));
          mailSenderImpl.setPort(env.getProperty(&quot;smtp.port&quot;, Integer.class));
          mailSenderImpl.setProtocol(env.getProperty(&quot;smtp.protocol&quot;));
          mailSenderImpl.setUsername(env.getProperty(&quot;smtp.username&quot;));
          mailSenderImpl.setPassword(env.getProperty(&quot;smtp.password&quot;));
          Properties javaMailProps = new Properties();
          javaMailProps.put(&quot;mail.smtp.auth&quot;, true);
          javaMailProps.put(&quot;mail.smtp.starttls.enable&quot;, true);
          mailSenderImpl.setJavaMailProperties(javaMailProps);
          return mailSenderImpl;
      }
  
      @Bean
      public CacheManager cacheManager() {
          return new ConcurrentMapCacheManager();
      }
  }
  </code></pre>
  <p>My controller (where there is an error setting up the controller)</p>
  <pre><code>@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 = &quot;/&quot;, method = RequestMethod.GET)
      public String home(Locale locale, Model model) {
          logger.info(&quot;Welcome home! The client locale is {}.&quot;, locale);
          
          Date date = new Date();
          DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
          
          String formattedDate = dateFormat.format(date);
          
          model.addAttribute(&quot;serverTime&quot;, formattedDate );
          
          return &quot;index.html&quot;;
      }
  }
  </code></pre>
  <p>What might be causing the <code>ConflictingBeanDefinitionException</code> error for my controller class?</p>