0

我收到此错误:

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 正在添加更多文本,因为我的帖子中有太多代码。您可以忽略此评论。

4

4 回答 4

2

如果您提供 UserService 的实现会更好。

确保使用@Service 注释实现。

于 2013-03-08T08:22:02.020 回答
0

@ComponentScan 仅适用于 javaConfig。您可以使用 Javaconfig 或 xml 配置,但无论如何您都需要将服务作为 spring bean 来管理!

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

于 2013-03-08T09:05:03.230 回答
0

您需要在 xml 中创建一个 bean UserService。

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

于 2013-03-08T08:21:52.407 回答
0

您的服务的实施情况如何?您是否使用 @Service 注释注释了您的 UserService 类和 UserService 的实现类?您也可以跳过 Controller 中的 getter&setter 并将 @Autowired 注释设置到您的字段,我知道它有效,但我不知道如何。

您还必须指示 Spring 在指定的包中搜索这些注释,这就是我的做法:

    <context:annotation-config/>
    <context:component-scan base-package="your_package"/>
    <mvc:annotation-driven/>
于 2013-03-08T08:22:11.267 回答