Java UnsatifiedPendencyException:创建名为';welcomeController';:通过字段'表示未满足的依赖关系;用户验证程序';
几个小时以来,我一直试图纠正这个错误,但收效甚微 我得到以下错误-Java UnsatifiedPendencyException:创建名为';welcomeController';:通过字段'表示未满足的依赖关系;用户验证程序';,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,几个小时以来,我一直试图纠正这个错误,但收效甚微 我得到以下错误- org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'welcomeController': Unsatisfied dependency expressed through field 'userValidator'; nested exception is org.springframe
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'welcomeController': Unsatisfied dependency expressed through field 'userValidator'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.config.UserValidator' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的WelcomeController有一个自动连接到用户验证程序-
@Controller
@RequestMapping("/")
public class WelcomeController {
@Autowired
AuctionItemRepository aucRepository;
@Autowired
UserRepository userRepository;
@Autowired
UserRoleRepository roleRepository;
@Autowired
UserValidator userValidator;
@Autowired
UserService userService;
@Autowired
SecurityService securityService;
// inject via application.properties
@Value("${welcome.message:test}")
private String message = "Hello World";
@RequestMapping(value = "/")
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";
}
Date getTime() {
Timestamp time = new Timestamp(new Date().getTime());
return time;
}
@RequestMapping(value = "/sell", method = RequestMethod.GET)
public String addAuction(Model model, HttpServletRequest request) {
model.addAttribute("newAuction", new AuctionItem());
model.addAttribute("action", "sell");
return "NewAuction";
}
@RequestMapping(value = "/sell", method = RequestMethod.POST)
public String saveAuction(@ModelAttribute("AuctionItem")AuctionItem auction, BindingResult result, ModelMap model){
if(result.hasErrors()){
return "error";
}
// User user = (User) userRepository.findByUsername(getUser());
auction.setAucStartTime(getTime());
aucRepository.save(auction);
// AuctionItem ai = aucRepository.findByOwnerAndStartTime(user, auction.getaStart());
return "redirect:/aucConfirm";
}
@RequestMapping(value = "/aucConfirm", method = RequestMethod.GET)
public String confirmAuction(Model model, HttpServletRequest request) {
return "aucConfirm";
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(Model model) {
model.addAttribute("userForm", new User());
return "register";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "register";
}
userService.save(userForm);
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model, String error, String logout) {
if (error != null)
model.addAttribute("error", "Your username and password is invalid.");
if (logout != null)
model.addAttribute("message", "You have been logged out successfully.");
return "login";
}
@控制器
@请求映射(“/”)
公共类WelcomeController{
@自动连线
拍卖项目和拍卖库;
@自动连线
用户存储库用户存储库;
@自动连线
用户角色扮演者角色扮演者;
@自动连线
用户验证程序用户验证程序;
@自动连线
用户服务用户服务;
@自动连线
证券服务证券服务;
//通过application.properties注入
@值(${welcome.message:test}”)
私有字符串message=“Hello World”;
@请求映射(value=“/”)
公共字符串欢迎(地图模型){
model.put(“message”,this.message);
返回“欢迎”;
}
日期获取时间(){
Timestamp time=新时间戳(new Date().getTime());
返回时间;
}
@RequestMapping(value=“/sell”,method=RequestMethod.GET)
公共字符串addAuction(模型,HttpServletRequest){
model.addAttribute(“newAuction”,newauctionItem());
模型。添加属性(“操作”、“出售”);
返回“新拍卖”;
}
@RequestMapping(value=“/sell”,method=RequestMethod.POST)
公共字符串saveAuction(@ModelAttribute(“AuctionItem”)AuctionItem拍卖,BindingResult,ModelMap模型){
if(result.hasErrors()){
返回“错误”;
}
//User=(User)userRepository.findByUsername(getUser());
setAucStartTime(getTime());
拍卖;保存(拍卖);
//AuctionItem ai=aucRepository.findByOwnerAndStartTime(用户,auction.getaStart());
返回“重定向:/auccomfirm”;
}
@RequestMapping(value=“/aucConfirm”,method=RequestMethod.GET)
公共字符串确认(模型,HttpServletRequest){
返回“确认”;
}
@RequestMapping(value=“/register”,method=RequestMethod.GET)
公共字符串寄存器(模型){
addAttribute(“userForm”,new User());
返回“登记簿”;
}
@RequestMapping(value=“/register”,method=RequestMethod.POST)
公共字符串寄存器(@modeldattribute(“userForm”)用户userForm,BindingResult,BindingResult,Model Model){
验证(userForm,bindingResult);
if(bindingResult.hasErrors()){
返回“登记簿”;
}
userService.save(userForm);
autologin(userForm.getUsername(),userForm.getPasswordConfirm());
返回“重定向:/”;
}
@RequestMapping(value=“/login”,method=RequestMethod.GET)
公共字符串登录(模型、字符串错误、字符串注销){
if(错误!=null)
addAttribute(“错误”,“您的用户名和密码无效。”);
如果(注销!=null)
addAttribute(“消息”,“您已成功注销”);
返回“登录”;
}
}
用户验证程序类-
public class UserValidator implements Validator{
@Autowired
private UserService userService;
@Override
public boolean supports(Class<?> aClass) {
return User.class.equals(aClass);
}
@Override
public void validate(Object o, Errors errors) {
User user = (User) o;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty");
if (user.getUsername().length() < 6 || user.getUsername().length() > 32) {
errors.rejectValue("username", "Size.userForm.username");
}
if (userService.findByUsername(user.getUsername()) != null) {
errors.rejectValue("username", "Duplicate.userForm.username");
}
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
if (user.getPass().length() < 8 || user.getPass().length() > 32) {
errors.rejectValue("password", "Size.userForm.password");
}
if (!user.getPasswordConfirm().equals(user.getPass())) {
errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm");
}
}
}
public类UserValidator实现验证器{
@自动连线
私人用户服务;
@凌驾
公共布尔支持(类aClass){
返回User.class.equals(aClass);
}
@凌驾
公共无效验证(对象o、错误){
用户=(用户)o;
ValidationUtils.rejectIfEmptyOrWhitespace(错误,“用户名”,“NotEmpty”);
if(user.getUsername().length()<6 | | user.getUsername().length()>32){
errors.rejectValue(“username”、“Size.userForm.username”);
}
if(userService.findByUsername(user.getUsername())!=null){
errors.rejectValue(“username”、“Duplicate.userForm.username”);
}
ValidationUtils.rejectIfEmptyOrWhitespace(错误,“密码”,“NotEmpty”);
if(user.getPass().length()<8 | | user.getPass().length()>32){
errors.rejectValue(“password”、“Size.userForm.password”);
}
如果(!user.getPasswordConfirm().equals(user.getPass())){
errors.rejectValue(“passwordConfirm”、“Diff.userForm.passwordConfirm”);
}
}
}
这些类以及我的Spring引导配置和WebSecurity配置都在com.config包中
有什么想法吗?您需要为
UserValidator
类使用@Component
原型注释,否则,Spring容器将不会实例化该类&因此将无法将其作为依赖项注入WelcomeController
中
@Component
public class UserValidator implements Validator{
//add current code
}
您的
UserValidator
似乎没有使用@Component
或类似内容进行注释。是的,这很有效,谢谢!