无法自动装配字段:private org.springframework.mail.javamail.JavaMailSender com.badmap.controller.SignUpController.javaMailSender;

时间:2015-11-15 07:58:48

标签: spring-boot

我的程序给了我一个错误“无法自动装配字段”,任何人都可以帮我这个吗?

@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)

1 个答案:

答案 0 :(得分:-1)

您的错误消息还提到没有符合条件的bean实现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,要么它不在范围内。