title: Spring bean getting initialised twice in java configuration
body: <p>I've created a spring application using spring-security with java based configuration. I've also included a jar file (created by me) in my project.
  The problem I am facing is:- i have to write <strong>@ComponentScan(basePackages = {"com.mypackage"})</strong> in both the classes (<strong>SpringConfig.java and SecurityConfig.java</strong>) which leads to initialization  of beans twice.
  Removing either of @componentscan leads to error:- <strong>Error creating bean with name 'securityConfig'</strong>.</p>
  
  <p>Below are my java classes.</p>
  
  <p><strong>SpringConfig.java</strong></p>
  
  <pre><code>package com.mypackage.config;
  
  import javax.annotation.PostConstruct;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  
  @EnableWebMvc
  @Configuration
  @ComponentScan(basePackages = {"com.mypackage"})
  public class SpringConfig extends WebMvcConfigurerAdapter {
      private static final Logger logger = LoggerFactory.getLogger(SpringConfig.class);
  
      @PostConstruct
      public void init(){
          logger.debug("Spring Config initialized");
      }
  }
  </code></pre>
  
  <p><strong>SecurityConfig.java</strong></p>
  
  <pre><code>package com.mypackage.config;
  
  import javax.annotation.PostConstruct;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  
  @Configuration
  @EnableWebSecurity
  @ComponentScan(basePackages = {"com.mypackage"})
  public class SecurityConfig extends WebSecurityConfigurerAdapter{
      private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);
  
      //This Configuration class is in my jar file.
      // with package starting with  same name com.mypackage
      @Autowired
      com.mypackage.frameworks.config.Configuration config;
  
      @PostConstruct
      public void init(){
          logger.debug("Security config initiaziled");
      }
  
      @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
      try {
          auth.inMemoryAuthentication()
              .withUser("admin").password("admin").roles("USER");
      } catch (Exception e) {
          e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p><strong>MyController.java</strong></p>
  
  <pre><code>package com.mypackage.controller;
  
  import javax.annotation.PostConstruct;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Controller;
  
  @Controller
  public class MyController {
      private static final Logger logger = LoggerFactory.getLogger(MyController.class);
  
      @PostConstruct
      public  void init(){
          logger.debug("-------Controller created-------");
      }
  }
  </code></pre>