title: spring mvc @Autowired error Unsatisfied 'required' dependency of type
body: <p>I have user service that treat user entity, and @Autowired in user controller class before use user service. so, I got the error: </p>
  
  <blockquote>
    <p><code>Unsatisfied 'required' dependency of type [class com.yes.service.UserService]. Expected at least 1 matching bean</code>  </p>
  </blockquote>
  
  <p>here the codes:</p>
  
  <p>userService </p>
  
  <pre><code>package com.yes.service;
  
  import java.util.List;
  import java.util.UUID;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.yes.domain.User;
  import com.yes.repository.RoleRepository;
  import com.yes.repository.UserRepository;
  
  @Service
  public class UserService {
  
  
      @Autowired
      private UserRepository userRepository;
      @Autowired
      private RoleRepository roleRepository;
  
      public User create(User user) {
          user.setId(UUID.randomUUID().toString());
          user.getRole().setId(UUID.randomUUID().toString());
          // We must save both separately since there is no cascading feature
          // in Spring Data MongoDB (for now)
          roleRepository.save(user.getRole());
          return userRepository.save(user);
      }
      public User read(User user) {
          return user;
      }
      public List&lt;User&gt; readAll() {
          return userRepository.findAll();
      }
      public User update(User user) {
          User existingUser = userRepository.findByUsername(user.getUserName());
          if (existingUser == null) {
              return null;
          }
          existingUser.setFirstName(user.getFirstName());
          existingUser.setLastName(user.getLastName());
          existingUser.getRole().setRole(user.getRole().getRole());
          // We must save both separately since there is no cascading feature
          // in Spring Data MongoDB (for now)
          roleRepository.save(existingUser.getRole());
          return userRepository.save(existingUser);
      }
      public Boolean delete(User user) {
          User existingUser = userRepository.findByUsername(user.getUserName());
          if (existingUser == null) {
              return false;
          }
          // We must delete both separately since there is no cascading feature
          // in Spring Data MongoDB (for now)
          roleRepository.delete(existingUser.getRole());
          userRepository.delete(existingUser);
          return true;
      }
  }
  </code></pre>
  
  <p>userController (where i use the userService, and the problem is)</p>
  
  <pre><code>@Controller
  @RequestMapping("/users")
  public class UserController {
  
      @Autowired
      private UserService service;
      @RequestMapping
      public String getUsersPage() {
          return "users";
      }
      @RequestMapping(value="/records")
      public @ResponseBody UserListDto getUsers() {
          UserListDto userListDto = new UserListDto();
          userListDto.setUsers(service.readAll());
          return userListDto;
      }
      @RequestMapping(value="/get")
      public @ResponseBody User get(@RequestBody User user) {
          return service.read(user);
      }
  
      @RequestMapping(value="/create", method=RequestMethod.POST)
      public @ResponseBody User create(
              @RequestParam String username,
              @RequestParam String password,
              @RequestParam String firstName,
              @RequestParam String lastName,
              @RequestParam Integer role) {
  
          Role newRole = new Role();
          newRole.setRole(role);
          User newUser = new User();
          newUser.setUserName(username);
          newUser.setPassword(password);
          newUser.setFirstName(firstName);
          newUser.setLastName(lastName);
          newUser.setRole(newRole);
          return service.create(newUser);
      }
      @RequestMapping(value="/update", method=RequestMethod.POST)
      public @ResponseBody User update(
              @RequestParam String username,
              @RequestParam String firstName,
              @RequestParam String lastName,
              @RequestParam Integer role) {
  
          Role existingRole = new Role();
          existingRole.setRole(role);
          User existingUser = new User();
          existingUser.setUserName(username);
          existingUser.setFirstName(firstName);
          existingUser.setLastName(lastName);
          existingUser.setRole(existingRole);
          return service.update(existingUser);
      }
      @RequestMapping(value="/delete", method=RequestMethod.POST)
      public @ResponseBody Boolean delete(
              @RequestParam String username) {
  
          User existingUser = new User();
          existingUser.setUserName(username);
          return service.delete(existingUser);
      }
  }
  </code></pre>
  
  <p>spring-data.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:p="http://www.springframework.org/schema/p"
      xmlns:c="http://www.springframework.org/schema/c" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mongo="http://www.springframework.org/schema/data/mongo"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-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/data/mongo
  http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-3.1.xsd"&gt;
  
      &lt;context:property-placeholder
          properties-ref="deployProperties" /&gt;
  
  
      &lt;!-- MongoDB host --&gt;
      &lt;mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}" /&gt;
  
      &lt;!-- Template for performing MongoDB operations --&gt;
      &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"
          c:mongo-ref="mongo" c:databaseName="${mongo.db.name}" /&gt;
  
      &lt;!-- Activate Spring Data MongoDB repository support --&gt;
      &lt;mongo:repositories base-package="com.yes.repository" mongo-template-ref="mongoTemplate"/&gt;
  
  
  
      &lt;!-- Service for initializing MongoDB with sample data using MongoTemplate --&gt;
      &lt;bean id="initMongoService" class="com.yes.service.InitMongoService" init-method="init"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>servlet-context.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
      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.xsd"&gt;
  
  &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
  
  &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
  &lt;annotation-driven /&gt;
  &lt;context:component-scan base-package="com.yes.controller"/&gt;
  
  
  
  &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt;
  &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
  &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;
  &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;beans:property name="prefix" value="WEB-INF/views/" /&gt;
      &lt;beans:property name="suffix" value=".jsp" /&gt;
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>applicationContext.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.xsd"&gt;
  &lt;context:property-placeholder
      properties-ref="deployProperties" /&gt;
  
  &lt;!-- Activates various annotations to be detected in bean classes --&gt;
  &lt;context:annotation-config /&gt;
  
  &lt;!-- Scans the classpath for annotated components that will be auto-registered 
      as Spring beans. For example @Controller and @Service. Make sure to set the 
      correct base-package --&gt;
  
      &lt;context:component-scan base-package="com.yes.domain"/&gt;
      &lt;context:component-scan base-package="com.yes.dto"/&gt;
      &lt;context:component-scan base-package="com.yes.service"/&gt;
  
  &lt;!-- Configures the annotation-driven Spring MVC Controller programming 
      model. Note that, with Spring 3.0, this tag works in Servlet MVC only! --&gt;
  &lt;mvc:annotation-driven /&gt;
  &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
  
  &lt;!-- Imports datasource configuration --&gt;
  &lt;import resource="spring-data.xml" /&gt;
  &lt;bean id="deployProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean"
      p:location="/WEB-INF/spring/spring.properties" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>the error stack:</p>
  
  <pre><code>ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.yes.service.UserService com.yes.controller.UserController.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.yes.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)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
      &lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Processes application requests --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>EDIT: the question is after fix the errors written in Sotirios Delimanolis answer and comment.</p>
  
  <p>What is the problem cause the error?</p>
  
  <p><strong>Answer: The problem was what was described in Sotirios Delimanolis answer. The Exact solution described in comments on his answer</strong></p>
  
  <p>thank you</p>