title: Spring Java configuration - Autowire issue
body: <p>Good day, </p>
  
  <p>I trying a simple test on Spring Autowire feature using java config approach, but i got the error as below, could anyone please shed some light on this, thanks ahead!</p>
  
  <p>Error:</p>
  
  <pre><code>  Error creating bean with name 'mycom.MyComTest': Injection of autowired    
    dependencies failed; nested exception is  
    org.springframework.beans.factory.BeanCreationException: Could not   
    autowire 
    field: private mycom.dao.AudioPlayer mycom.MyComTest.iModel; nested   
    exception is 
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No  
    qualifying bean of type [mycom.dao.AudioPlayer] found for dependency: 
    expected at least 1 bean which qualifies as autowire candidate for this 
    dependency. Dependency annotations: 
    {@org.springframework.beans.factory.annotation.Autowired(required=true)}    
  </code></pre>
  
  <p>AudioPlayer Interface: </p>
  
  <pre><code>package mycom.dao;
  
    public interface AudioPlayer {
    void play();
  }
  </code></pre>
  
  <p>CDPlayer class: </p>
  
  <pre><code>package mycom.dao;
  
  import org.springframework.stereotype.Component;
  
  @Component
  public class CDPlayer implements AudioPlayer {
  
     @Override
     public void play() {
      System.out.println("this is playing by CD....");
  
   }
  
  }
  </code></pre>
  
  <p>Webconfig - define beans </p>
  
  <pre><code> package mycom.init;
   import java.util.Locale;
   import mycom.MyComTest;
   import org.springframework.context.MessageSource;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.ComponentScan;
   import org.springframework.context.annotation.Configuration;
   import org.springframework.context.support.ResourceBundleMessageSource;
   import org.springframework.web.servlet.LocaleResolver;
   import org.springframework.web.servlet.config.annotation.EnableWebMvc;
   import  
   org.springframework.web.servlet.config.annotation.InterceptorRegistry;
   import 
   org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
   import 
   org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
   import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
   import org.springframework.web.servlet.i18n.SessionLocaleResolver;
   import org.springframework.web.servlet.view.InternalResourceViewResolver;
  
   @Configuration
   @EnableWebMvc
   @ComponentScan(basePackages="mycom.*")
   public class WebConfig extends WebMvcConfigurerAdapter{
  
   @Bean
   public InternalResourceViewResolver getInternalResourceViewResolver(){
      InternalResourceViewResolver resolver = new   
      InternalResourceViewResolver();
      resolver.setPrefix("/WEB-INF/view/");
      resolver.setSuffix(".jsp");
      return resolver;
   }         
  }
  </code></pre>
  
  <p>WebInitalizaer - define Application Context: package mycom.init;</p>
  
  <pre><code> import javax.servlet.ServletContext;
   import javax.servlet.ServletException;
   import javax.servlet.ServletRegistration;
  
   import org.springframework.web.WebApplicationInitializer;
   import org.springframework.web.context.ContextLoaderListener;
   import org.springframework.web.context.WebApplicationContext;
   import       
  </code></pre>
  
  <p>org.springframework.web.context.support.AnnotationConfigWebApplicationContext;</p>
  
  <pre><code> import org.springframework.web.servlet.DispatcherServlet;
  
  
   public class WebInitializer implements WebApplicationInitializer  {
  
   @Override
   public void onStartup(ServletContext servletContext) throws  
   ServletException {
      WebApplicationContext context = getContext();
      servletContext.addListener(new ContextLoaderListener(context));
      ServletRegistration.Dynamic dispatcher = 
      servletContext.addServlet("DispatcherServlet", new   
      DispatcherServlet(context));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
      dispatcher.addMapping("*.jsp"); 
      dispatcher.addMapping("*.json");
      dispatcher.addMapping("*.xml");
    }
  
      private AnnotationConfigWebApplicationContext getContext() {
             AnnotationConfigWebApplicationContext context = new   
             AnnotationConfigWebApplicationContext();
            context.setConfigLocation("mycom.init.WebConfig");
            return context;
      } 
     }
  </code></pre>
  
  <p>Test case: 
                      package mycom;</p>
  
  <pre><code>            import org.junit.Test;
              import org.junit.runner.RunWith;
              import org.springframework.beans.factory.annotation.Autowired;
              import org.springframework.test.context.ContextConfiguration;
              import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  
  
  
  
              import mycom.dao.AudioPlayer;
              import mycom.init.WebInitializer;
  
  
              @RunWith(SpringJUnit4ClassRunner.class)
              @ContextConfiguration(classes=WebInitializer.class)
              public class MyComTest {
  
                  @Autowired //I tried to change @Bean here but not working
                  private AudioPlayer iModel;
  
                  @Test
                  public void play(){
                      System.out.println("testing ...");
                      iModel.play();
                  }
              }
  </code></pre>