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