title: Spring Autowiring Service doesn't work in my Controller
body: <p>i've problems in order to autowire a service in my controller. I've this error:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private es.unican.meteo.service.UserService es.unican.meteo.controller.MyController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [es.unican.meteo.service.UserService] 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>It seems that the userService is not registered, so that, the controller can't get the bean. I thought that my config was ok because it works with the tests. In the tests i have this:</p>
  
  <pre><code>ClassPathXmlApplicationContext("/WEB-INF/app-config.xml");
  </code></pre>
  
  <p>and i can get the bean ok from the ApplicationContext.xml</p>
  
  <p>My package structure is the following:</p>
  
  <p>es.unican.meteo.controller</p>
  
  <p>|---- MyController.java</p>
  
  <p>es.unican.meteo.service</p>
  
  <p>|---- UserService.java</p>
  
  <p>es.unican.meteo.service.impl</p>
  
  <p>|---- UserServiceImpl.java</p>
  
  <p>.....</p>
  
  <p>WebContent/WEB-INF</p>
  
  <p>|---- MyDispatcherServlet-servlet.xml</p>
  
  <p>|---- app-config.xml</p>
  
  <p>|---- web.xml</p>
  
  <p>.....</p>
  
  <p>The clases:</p>
  
  <p>== UserServiceImpl.java ==</p>
  
  <pre><code>@Service
  public class UserServiceImpl implements UserService{
  
      @Autowired
      private UserMapper userMapper;
  
      public void setUserMapper(UserMapper userMapper) {
          this.userMapper = userMapper;
      }
  </code></pre>
  
  <p>== MyController.java ==</p>
  
  <pre><code>@Controller
  public class MyController {
  
       @Autowired
       private UserService userService;
  
       @RequestMapping(method=RequestMethod.GET, value="/home")
       public String handleRequest(){
        return "welcome";
       }
  
       @RequestMapping(method=RequestMethod.GET, value="/getUsers")
       public @ResponseBody List&lt;User&gt; getUsersInJSON(){
        return userService.getUsers();
       }
  }
  </code></pre>
  
  <p>== web.xml ==</p>
  
  <pre><code>&lt;display-name&gt;Spring MVC&lt;/display-name&gt;
  
   &lt;servlet&gt;
    &lt;servlet-name&gt;MyDispatcherServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
   &lt;/servlet&gt;
  
   &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;MyDispatcherServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;*.go&lt;/url-pattern&gt;
   &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>== app-config.xml ===</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;
  
  
     &lt;!-- Scans the classpath of this application for @Components to deploy as beans --&gt;
     &lt;context:component-scan base-package="es.unican.meteo" /&gt;
  
     &lt;!-- Configures the @Controller programming model --&gt;
     &lt;mvc:annotation-driven/&gt;
  
     &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
      p:url="jdbc:derby:C:\tools\derbydb"
      p:connectionProperties=""
      p:username="APP"
      p:password="" /&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="configLocation" value="/mybatis-config.xml" /&gt;
      &lt;/bean&gt; 
  
      &lt;bean id="usersMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"&gt;
          &lt;property name="mapperInterface" value="es.unican.meteo.dao.UserMapper" /&gt;
          &lt;property name="sqlSessionFactory" ref="sqlSessionFactory" /&gt;
      &lt;/bean&gt; 
  
      &lt;bean id="rolesMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"&gt;
          &lt;property name="mapperInterface" value="es.unican.meteo.dao.RoleMapper" /&gt;
          &lt;property name="sqlSessionFactory" ref="sqlSessionFactory" /&gt;
      &lt;/bean&gt; 
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>== MyDispatcherServlet.xml ==</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;
  
   &lt;!-- Enabling Spring beans auto-discovery --&gt;
   &lt;context:component-scan base-package="es.unican.meteo.controller" /&gt;
  
   &lt;!-- Enabling Spring MVC configuration through annotations --&gt;
   &lt;mvc:annotation-driven /&gt;
  
   &lt;!-- Defining which view resolver to use --&gt;
   &lt;bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" &gt; 
    &lt;property name="prefix" value="/WEB-INF/views/" /&gt; 
    &lt;property name="suffix" value=".jsp" /&gt; 
   &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>Spring mvc logger trace:</p>
  
  <pre><code>19:38:54,119 DEBUG http-8080-1 support.DefaultListableBeanFactory:430 - Creating instance of bean 'myController'
  19:38:54,170 DEBUG http-8080-1 annotation.InjectionMetadata:60 - Found injected element on class [es.unican.meteo.controller.MyController]: AutowiredFieldElement for private es.unican.meteo.service.UserService es.unican.meteo.controller.MyController.userService
  19:38:54,174 DEBUG http-8080-1 support.DefaultListableBeanFactory:504 - Eagerly caching bean 'myController' to allow for resolving potential circular references
  19:38:54,206 DEBUG http-8080-1 annotation.InjectionMetadata:85 - Processing injected method of bean 'myController': AutowiredFieldElement for private es.unican.meteo.service.UserService es.unican.meteo.controller.MyController.userService
  19:38:54,224 DEBUG http-8080-1 support.DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'userServiceImpl'
  19:38:54,226 DEBUG http-8080-1 support.DefaultListableBeanFactory:430 - Creating instance of bean 'userServiceImpl'
  19:38:54,234 DEBUG http-8080-1 annotation.InjectionMetadata:60 - Found injected element on class [es.unican.meteo.service.impl.UserServiceImpl]: AutowiredFieldElement for private es.unican.meteo.dao.UserMapper es.unican.meteo.service.impl.UserServiceImpl.userMapper
  19:38:54,237 DEBUG http-8080-1 support.DefaultListableBeanFactory:504 - Eagerly caching bean 'userServiceImpl' to allow for resolving potential circular references
  19:38:54,256 DEBUG http-8080-1 annotation.InjectionMetadata:85 - Processing injected method of bean 'userServiceImpl': AutowiredFieldElement for private es.unican.meteo.dao.UserMapper es.unican.meteo.service.impl.UserServiceImpl.userMapper
  19:38:54,268  INFO http-8080-1 support.DefaultListableBeanFactory:433 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@56088b29: defining beans [myController,roleService,userServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
  19:38:54,279 ERROR http-8080-1 servlet.DispatcherServlet:457 - Context initialization failed
  </code></pre>
  
  <p>I've reviewed some questions about this topic but i don't find a solution to my problem. Maybe i'm skipping something but i don't know certainly. I tried to change the component-scan with no results.</p>
  
  <p>When i try to access to /SPRING-MVC/getUsers.go appears those errors.</p>
  
  <p>I don't know if the beans must be placed in app-config (applicationContext) or in the servlet.xml because it is a little bit confusing...</p>
  
  <p>Thank you</p>