我喜欢春天,我喜欢那些注释,为我节省了很多时间。但它也让我头脑发热。那么有人可以向我解释@Autowired plz吗?
我遵循了一些教程,这是我得到的:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registerController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.isad.dao.UserAccountDAO
com.isad.controller.RegisterController.userAccount; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.isad.dao.UserAccountDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的Pojo:
private int id;
private String username;
private String email;
private String password;
我的道:
@Repository
@Transactional
public class UserAccountDAOImpl implements UserAccountDAO{
@Autowired
private SessionFactory sessionFactory;
public UserAccount getById(int id) {
return (UserAccount) sessionFactory.getCurrentSession()
.get(UserAccount.class, id);
}
public int save(UserAccount aUser) {
return (Integer)sessionFactory.getCurrentSession().save(aUser);
}
}
我的控制器:
@Autowired
private UserAccountDAO userAccount;
@Autowired
private UserAccountFormValidator validator;
@RequestMapping(value="register", method=RequestMethod.POST)
public String registerPage(@ModelAttribute("newUserAccount") UserAccount aUser,
BindingResult result) {
System.out.println("inside register post");
validator.validate(aUser, result);
if( result.hasErrors()) {
return "register";
}
userAccount.save( aUser );
return "../../index";
}
UserAccountFormValidator:
@Component("useraccountFormValidator")
public class UserAccountFormValidator implements Validator{
@Override
public boolean supports(Class<?> arg0) {
return UserAccount.class.isAssignableFrom( arg0 );
}
@Override
public void validate(Object arg0, Errors arg1) {
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "name", "required");
}
}
答案 0 :(得分:0)
可能没有针对Spring bean扫描com.isad.dao
的包UserAccountDAOImpl
。检查你的Spring配置。
答案 1 :(得分:0)
确保您的spring配置文件提供:
<context:component-scan base-package="com.isad"/>