簡體   English   中英

在Spring中自動發送錯誤:注入自動連接的依賴項失敗

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

我收到這個錯誤:

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

服務存在:

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

我已將此添加到xml中

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

我添加了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正在添加更多文本,因為我的帖子中有太多代碼。 你可以忽略這個評論。

如果您提供UserService的實現會更好。

確保使用@Service注釋實現。

您需要在xml中創建一個bean UserService。

或者你的控制器找不到它!

如何實施您的服務? 您是否使用@Service注釋注釋了UserService類和UserService的實現類? 您也可以在Controller中跳過getter&setter並將@Autowired注釋設置為您的字段,我知道它有效,但我不知道如何。

您還必須指示Spring在指定的包中搜索這些注釋,這就是我的方法:

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

@ComponentScan僅適用於javaConfig。 您可以使用Javaconfig或xml配置,但無論如何您需要將服務作為spring bean進行管理!

資料來源: http//static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/ComponentScan.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM