Spring-boot + mail / Session JNDI resource = NoSuchBeanDefinitionException(JavaMailSender)

时间:2016-06-01 14:39:22

标签: spring spring-boot javamail jndi tomcat8

我已经在将邮件/会话配置为JNDI资源(在Tomcat端声明)上进行了几天(并且没有进展)的困难,并被Spring-boot应用程序选中。我们的想法是避免在应用程序中包含任何关键信息,例如用户/密码数据。

注意1 :已通过SMTP测试凭据和连接,应用程序端配置在application.properties中进行了硬编码,因此这不是问题。)

我使用的当前配置如下:

server.xml ($ TOMCAT / conf)

<Resource
   name="mail/Session"
   auth="Container"
   type="javax.mail.Session"
   username="user@test.com"
   password="XXX"
   mail.user="user@test.com"
   mail.password="XXX"
   mail.transport.protocol="smtp"
   mail.smtp.host="smtp.test.com"
   mail.smtp.auth="true"
   mail.smtp.port="587"/>

context.xml (webapp / META-INF)

<Context>
    <ResourceLink 
        name="mail/Session"
        global="mail/Session"
        auth="Container"
        type="javax.mail.Session"/>
</Context>

application.properties

spring.mail.jndi-name=java:comp/env/mail/Session

pom.xml 中,我有spring-boot-starter-mail依赖项(以及其他必要的依赖项)。我正在使用JavaMailSender bean的类就是这个(简化):

注2 :使用的spring-boot版本是1.2.8.RELEASE)

@Service("emailService")
public class EmailService {

    @Autowired
    public JavaMailSender javaMailSender;

    public void sendSimpleMail(/* ... */) throws MessagingException {

        //Create message
        MimeMessage message = javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setFrom(fromAddress);
        helper.setTo(toAddress);
        helper.setSubject(subject);
        helper.setText(body);

        //Send message
        javaMailSender.send(message);
    }
}

我知道spring-boot自动配置应该在找到jndiName或主机,端口,用户名,密码... spring.mail properties后创建JavaMailSender bean(后者在设置主机,端口,用户名和密码时起作用)在application.properties)中,但是当我使用jndiName选项时它会抛出以下堆栈:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emailService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.mail.javamail.JavaMailSender com.testapp.service.EmailService.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:1208)
    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.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
    at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:135)
    at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:126)
    at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:82)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5240)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    ... 10 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.mail.javamail.JavaMailSender com.testapp.service.EmailService.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

我以相同的方式将dataSource配置为JNDI资源,并且它完美地运行。 (邮件)会话是否需要一个不同的方法,或者我错过了它的配置?

1 个答案:

答案 0 :(得分:0)

正如@m-deinum在评论中指出的那样,问题不在于配置,而是在1.3.x下的spring-boot版本中缺少JNDI支持

为了解决这个问题,我最终注意到 @ m-deinum 的提示并将spring-boot从1.2.8升级到1.3.5 ...处理一些已弃用的问题课程和新配置,随时可用!