[英]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.