简体   繁体   中英

Autowired in Spring giving an error : Injection of autowired dependencies failed

I'm getting this error:

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)

Controller class

  @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";
}

service is existing:

  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;
 }

I've added this to the xml

 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

I've added the UserServiceImpl

    public class UserServiceImpl extends BaseServiceImpl<User> 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<DietCategory> 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<User> getAllUser() throws ServiceException {
    return findAll(User.class);
}

stackoverflow is making add more text because there is too much code in my post. you could ignore this comment.

It would be better if you provided an implementation of UserService.

Make sure the implementation is annotated with @Service.

You need to create a bean UserService in your xml.

Or your controller can't find it !

What about implementation of your Service? Did you annotated your UserService class and UserService's implementation class with @Service annotation? You can also skip getter&setter in Controller and set @Autowired annotation to your field, I know that it works, but I have no idea how.

You have also to instruct Spring to search for those annotations in specified packages, this is how I do:

    <context:annotation-config/>
    <context:component-scan base-package="your_package"/>
    <mvc:annotation-driven/>

The @ComponentScan will only work on a javaConfig. Either you use the Javaconfig or the xml configuration but anyways you will need to manage your service as a spring bean !

Source : http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/ComponentScan.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM