2015-11-15 77 views
1

我試圖使用spring boot來發送電子郵件。但是,我總是得到NoSuchBeanDifinition的警告。我知道javaemailsender是來自導入API的引用,它不是帶有@Bean的類。任何人都可以幫助我嗎?無法自動裝入字段:private org.springframework.mail.javamail.JavaMailSender

@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", "[email protected]"); 


     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 
+0

的[無法自動裝配字段:私人org.springframework.mail.javamail.JavaMailSender com.badmap.controller.SignUpController.javaMailSender;]可能的複製(http://stackoverflow.com/questions/33717596/could -not-autowire-field-private-org-springframework-mail-javamail-javamailsend) – dunni

+0

這確實是一個重複的問題,但這裏沒有有用的答案,也沒有有用的答案。 – Amnon

回答

2

如果使用彈簧啓動,你可以檢查你的配置。

spring.mail.host=smtp.xxx.com 
[email protected] 
spring.mail.password=xxxx 
spring.mail.properties.mail.smtp.auth=true 
+0

這工作對我來說,我得到了原始文章中提到的確切異常,並在我的application.properties中添加這個修復了自動裝配問題 – Jason

相關問題