无法自动装配字段:private org.springframework.mail.javamail.JavaMailSender

时间:2015-11-15 06:14:01

标签: spring-boot

我试图使用spring boot发送电子邮件。但是,我总是得到NoSuchBeanDifinition的警告。我知道javaemailsender是导入的API的引用,它不是带@Bean的类。任何人都可以帮我吗?

@RestController
public class SignUpController {

    @Autowired
    private UserRepository repo;

    @Autowired
    private SendingEmails sendingEmail;

    @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();
        sendingEmail.send("haha", "success of registration", "xy_jonathan@hotmail.com");


        return repo.save(newUser);

    }



    @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Email already exists")
    public class DuplicateEmailException extends RuntimeException {
    }

    @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Username already exists")
    public class DuplicateUsernameException extends RuntimeException {
    }
}


@Service
public class SendingEmails {

    @Autowired
    private JavaMailSender javaMailSender;

    public void send(String subject, String to, String text) throws MessagingException{
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper;

        helper = new MimeMessageHelper(message,true);

        helper.setSubject(subject);
        helper.setTo(to);
        helper.setText(text);

        javaMailSender.send(message);
    }
}

错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.badmap.model.SendingEmails com.badmap.controller.SignUpController.sendingEmail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendingEmails': 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.model.SendingEmails.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 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendingEmails': 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.model.SendingEmails.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)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 18 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.mail.javamail.JavaMailSender com.badmap.model.SendingEmails.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)
    ... 29 more
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)
    ... 31 more

1 个答案:

答案 0 :(得分:2)

如果你使用spring-boot,你可以查看你的配置。

spring.mail.host=smtp.xxx.com
spring.mail.username=xxxx@xxx.com
spring.mail.password=xxxx
spring.mail.properties.mail.smtp.auth=true