【发布时间】:2016-10-01 00:11:40
【问题描述】:
我已经为将邮件/会话配置为 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);
}
}
我知道一旦找到 jndiName 或主机、端口、用户名、密码...spring.mail properties,spring-boot 自动配置应该创建 JavaMailSender bean(后者在我在应用程序中设置主机、端口、用户名和密码时起作用.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
我有一个以相同方式配置为 JNDI 资源的数据源,它运行良好。 (mail)Session 需要不同的方法还是我在它的配置上遗漏了什么?
【问题讨论】:
-
jndi 已在 Spring Boot 1.3 中添加,因此不适用于 1.2。
-
该死...我知道您指的是 JNDI 对 JavaMailSender Bean 自动配置的支持(否则我的应用程序将无法连接到我的数据库)。那么这是否意味着为了初始化 bean,我需要手动进行,而如果我在应用程序中硬编码设置属性,bean 初始化会自动完成? @.ô
-
是或者升级spring boot :)。
-
会做,只是希望不要在过程中发现太多问题。谢谢兄弟!
标签: spring spring-boot jakarta-mail jndi tomcat8