title: Cannot use Components/Service/Repository in Controller class
body: <p>I have a well configurated dao layer(I've created test to check it), and now I would like to integrate my project with spring mvc.</p>
  
  <p>I've added this configuration:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = "com.szymon.controller")
  public class WebMvcConfig {
          @Bean
          public ViewResolver viewResolver() {
              InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
              viewResolver.setViewClass(JstlView.class);
              viewResolver.setPrefix("/WEB-INF/views/");
              viewResolver.setSuffix(".jsp");
  
              return viewResolver;
          }
      }
  
  public class WebInitializer implements WebApplicationInitializer {
  
      public void onStartup(ServletContext container) throws ServletException {
  
          AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
          ctx.register(WebMvcConfig.class);
          ctx.setServletContext(container);
  
          ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
  
          servlet.setLoadOnStartup(1);
          servlet.addMapping("/");
      }
  
  }
  </code></pre>
  
  <p>And my controller looks like:</p>
  
  <pre><code>@Controller
  public class HelloWorldController {
  
      @Autowired
      private MeetingRepository meetingRepository;
  
  
      @RequestMapping("/hello")
      public String helloWorld(Model model) {
  model.addAttribute("message", "message");
      model.addAttribute("meeting", meetingRepository.findOne(1L));
      return "helloWorld";
  }
  
  @RequestMapping("/hel")
  public String hel(Model model) {
      model.addAttribute("message", "message");
      return "helloWorld2";
  }
  </code></pre>
  
  <p>}</p>
  
  <p>After commenting meetingRepository field and helloWorld() method servlet works fine. But if its not commented it doesn't working:</p>
  
  <pre><code>javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      java.lang.Thread.run(Thread.java:745)
  root cause
  
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorldController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.szymon.repository.MeetingRepository com.szymon.controller.HelloWorldController.meetingRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.szymon.repository.MeetingRepository] 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)}
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
      org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:664)
      org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:536)
      org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:490)
      org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      javax.servlet.GenericServlet.init(GenericServlet.java:158)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      java.lang.Thread.run(Thread.java:745)
  root cause
  
  org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.szymon.repository.MeetingRepository com.szymon.controller.HelloWorldController.meetingRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.szymon.repository.MeetingRepository] 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)}
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
      org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
      org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:664)
      org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:536)
      org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:490)
      org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      javax.servlet.GenericServlet.init(GenericServlet.java:158)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      java.lang.Thread.run(Thread.java:745)
  root cause
  
  org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.szymon.repository.MeetingRepository] 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)}
      org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
      org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
      org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:664)
      org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:536)
      org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:490)
      org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      javax.servlet.GenericServlet.init(GenericServlet.java:158)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
      org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>It looks, that spring is not making beans when using spring mvc. How should i configure my application for working properly?</p>
  
  <p><strong>EDIT</strong> 
  MeetingRepository class:</p>
  
  <pre><code>public interface MeetingRepository extends JpaRepository&lt;Meeting, Long&gt; {
  }
  
  @Configuration
  @EnableJpaRepositories(basePackages = "com.szymon.repository")
  @EnableTransactionManagement
  public class DataAccessConfig {
  
      @Bean
      public LocalEntityManagerFactoryBean entityManagerFactory() {
          LocalEntityManagerFactoryBean localEntityManagerFactoryBean = new LocalEntityManagerFactoryBean();
          localEntityManagerFactoryBean.setPersistenceUnitName("postgres");
          return localEntityManagerFactoryBean;
      }
  
      @Bean
      public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
          JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
          jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
          return jpaTransactionManager;
      }
  }
  </code></pre>
  
  <p>It's working well, because I've tested it:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = DataAccessConfig.class)
  public class MeetingRepositoryTest {
  
      @Autowired
      private MeetingRepository meetingRepository;
  
      @Test
      public void test() {
          Meeting meeting = new Meeting();
          meeting.setAddedDate(new Date());
          meeting.setMeetingDate(new Date(System.currentTimeMillis() + 604800000));
          meeting.setNotes("Moje pierwsze spotkanie.");
          meeting.setPlace("Pokoj 200");
  
          meetingRepository.save(meeting);
          Assert.assertTrue(true);
      }
  
  
  }
  </code></pre>