title: Autowired in Spring giving an error : Injection of autowired dependencies failed
body: <p>I'm getting this error:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newStep2Controller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void projecthealth.web.NewStep2Controller.setUserService(projecthealth.service.UserService); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [projecthealth.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.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
  </code></pre>
  
  <p>Controller class</p>
  
  <pre><code>  @Controller
    @RequestMapping("/newStep2.htm")
    @SessionAttributes("user")
    @ComponentScan("projecthealth.service")
   public class NewStep2Controller
   {
   protected final Log logger = LogFactory.getLog(getClass());
   private UserService userService;
  
  @Autowired
  public void setUserService(UserService userService) {
      this.userService = userService;
  }
     @RequestMapping(method = RequestMethod.GET)
  public String showUserForm(ModelMap model)
  {
      model.addAttribute("user");
  
      return "userForm";
  }
  </code></pre>
  
  <p>service is existing:</p>
  
  <pre><code>  public interface UserService {
  
  void createUser(User user)  throws ServiceException;
  
  /**
   * 
   * @param userId (email is user id)
   * @return
   * @throws ServiceException
   */
  User getUserById(String userId)  throws ServiceException;
  
  void deleteUser(String userId)  throws ServiceException;
  
  /**
   * 
   * @param newUserObject
   * @param userId (email is user id)
   * @return
   * @throws ServiceException
   */
  User updateUser(User newUserObject, String userId)  throws ServiceException;
   }
  </code></pre>
  
  <p>I've added this to the xml</p>
  
  <pre><code> &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&gt;
  </code></pre>
  
  <p>I've added the UserServiceImpl</p>
  
  <pre><code>    public class UserServiceImpl extends BaseServiceImpl&lt;User&gt; implements UserService{
  
  public static final String FIELD_EMAIL = "email";
  
  public void createUser(User user) throws ServiceException {
      insert(user);
  }
  
  public User getUserById(String userId) throws ServiceException {
      return (User) findOne(User.class, FIELD_EMAIL, userId);
  }
  
  public void deleteUser(String userId) throws ServiceException {
      delete(User.class, FIELD_EMAIL, userId);
  }
  
  public User updateUser(User newUserObject, String oldEmail) throws ServiceException {
      MongoTemplate template = getTemplate();
      User userObject = getUserById(oldEmail);
  
      List&lt;DietCategory&gt; dietaryPreferences = newUserObject.getDietaryPreferences();
  
      if(dietaryPreferences != null){
          userObject.setDietaryPreferences(dietaryPreferences);
      }
      userObject.setEmail(newUserObject.getEmail());
      userObject.setFirstname(newUserObject.getFirstname());
      userObject.setHeight(newUserObject.getHeight());
      userObject.setLastname(newUserObject.getLastname());
      userObject.setPassword(newUserObject.getPassword());
      userObject.setWeight(newUserObject.getWeight());
      template.save(userObject);
      return newUserObject;
  }
  
  public List&lt;User&gt; getAllUser() throws ServiceException {
      return findAll(User.class);
  }
  </code></pre>
  
  <p>stackoverflow is making add more text because there is too much code in my post. 
  you could ignore this comment.</p>