本文介绍了无法自动装配字段:private org.springframework.mail.javamail.JavaMailSender com.badmap.controller.SignUpController.javaMailSender;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序给我一个错误无法自动装配字段",谁能帮我解决这个问题?

My program gives me an error "Could not autowire field," can anyone help me with this?

@RestController
public class SignUpController {

    @Autowired
    private UserRepository repo;

    @Autowired
    private JavaMailSender javaMailSender;

    @RequestMapping(method = RequestMethod.POST, value = "/signup")
    public @ResponseBody
    Object create(@Valid @RequestBody User user, BindingResult result, Errors error) throws MessagingException {
        //Check the result, if error, return error and don't add user
        System.out.println("Result: " + result);
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            if (fieldError.getField().equals("email")) {
                throw new DuplicateEmailException();
            }
            //Lazy Else
            throw new DuplicateUsernameException();
    }

    User newUser = User.createUser(user.getUsername(), user.getEmail(), user.getPassword());

    //String emailMessage = "Thank you, you username is"+user.getUsername();
    SimpleMailMessage mailmessage = new SimpleMailMessage();
    mailmessage.setTo(newUser.getEmail());
    mailmessage.setSubject("Registration");
    mailmessage.setText("Hello " +newUser.getUsername() +"\n Your registration is successfull");

    javaMailSender.send(mailmessage);
    return repo.save(newUser); 

}

输出如下:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.mail.javamail.JavaMailSender com.badmap.controller.SignUpController.javaMailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 16 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 18 common frames omitted

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'signUpController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.mail.javamail.JavaMailSender com.badmap.controller.SignUpController.javaMailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)

推荐答案

您的错误消息还提到没有实现 JavaMailSender

Your error message also mentions that there's no qualifying bean that implements JavaMailSender

NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] 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)}

您要么没有实现 JavaMailSender 的 bean,要么不在范围内.

You either don't have a bean that implements JavaMailSender or it is not in scope.

这篇关于无法自动装配字段:private org.springframework.mail.javamail.JavaMailSender com.badmap.controller.SignUpController.javaMailSender;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 23:15