我的Java Spring Boot项目中有一个emailService实现。要发送电子邮件,我使用JavaMailSender。在我的服务中,我需要访问HttpServletRequest
和HttpServletResponse
才能实例化WebContext。现在我通过我的mailsender
方法传递这些2 - 请求和响应听起来像个坏主意。我希望将它们放在我的服务中,这样我就可以在我的mailsender
中调用只有2个变量的方法:要使用的电子邮件模板和将在该电子邮件上打印的变量映射。有没有办法在我的服务中实例化请求和响应?我可以以某种方式在服务中自动装配它们吗?这就是我的服务的样子:
@Service
@Qualifier("MailSender")
public class MailSenderService {
@Autowired
private JavaMailSender mailSender;
@Autowired
private ServletContextTemplateResolver emailTemplateResolver;
public boolean sendMail(HttpServletRequest request, HttpServletResponse
response, HashMap<String, String> info, String template) throws
MessagingException, IOException{
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new
MimeMessageHelper(mimeMessage,true, "UTF-8"); // true = multipart
message.setFrom("sender@example.com");
message.setTo("mymail@example.com");
message.setSubject("This is the message subject");
TemplateEngine engine = new TemplateEngine();
engine.setTemplateResolver(emailTemplateResolver);
WebContext ctx = new WebContext(request, response,
request.getServletContext(), request.getLocale());
ctx.setVariable("info", info);
try{
String messageContent= engine.process(template, ctx);
mimeMessage.setContent(tt, "text/html; charset=utf-8");
}catch(Exception e){
e.printStackTrace();
}
this.mailSender.send(mimeMessage);
return true;
}
}
答案 0 :(得分:1)
如果类是请求作用域,您可以按如下方式自动装配:
@Autowired private HttpServletRequest request;
干杯..
答案 1 :(得分:1)
您应该可以自动加载HttpServletRequest
,尽管同样不适用于HttpServletResponse
。
例如
@Autowired HttpServletRequest request; // works
@Autowired HttpServletResponse response; // doesn't work
有一些关于here的讨论 - 但最后看起来你可能不得不手动传递回复。
答案 2 :(得分:1)
对于初学者,您MailSenderService
不应该为创建TemplateEngine
而烦恼,也不应该知道如何解析模板,这应该是透明的。请参阅Thymeleaf网站上的this turorial。
假设您正在使用XML进行bean配置,而Spring 4则添加以下内容。 (您可能已经配置了ServletContextTemplateResolver
的实例。)
<bean id="webTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="2" />
</bean>
<!-- THYMELEAF: Template Engine (Spring3-specific version) -->
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolvers">
<set>
<ref bean="emailTemplateResolver" />
<ref bean="webTemplateResolver" />
</set>
</property>
</bean>
现在,在您的MailSenderService
中,您只需注入TemplateEngine
。
@Service
@Qualifier("MailSender")
public class MailSenderService {
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine engine;
public boolean sendMail(HttpServletRequest request, HttpServletResponse response, HashMap<String, String> info, String template) throws MessagingException, IOException{
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new
MimeMessageHelper(mimeMessage,true, "UTF-8"); // true = multipart
message.setFrom("sender@example.com");
message.setTo("mymail@example.com");
message.setSubject("This is the message subject");
WebContext ctx = new WebContext(request, response,
request.getServletContext(), request.getLocale());
ctx.setVariable("info", info);
try{
String messageContent= engine.process(template, ctx);
mimeMessage.setContent(tt, "text/html; charset=utf-8");
}catch(Exception e){
e.printStackTrace();
}
this.mailSender.send(mimeMessage);
return true;
}
}
而不是request.getLocale()
只需使用LocaleContextHolder.getLocale()
,并将模板中所需的所有属性放在info
变量中。有了这个,您可以将方法重写为以下内容。
public boolean sendMail(Map<String, String> info, String template) throws MessagingException, IOException{
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new
MimeMessageHelper(mimeMessage,true, "UTF-8"); // true = multipart
message.setFrom("sender@example.com");
message.setTo("mymail@example.com");
message.setSubject("This is the message subject");
Context ctx = new Context(LocaleContextHolder.getLocale());
ctx.setVariable("info", info);
try{
String messageContent= engine.process(template, ctx);
mimeMessage.setContent(tt, "text/html; charset=utf-8");
}catch(Exception e){
e.printStackTrace();
}
this.mailSender.send(mimeMessage);
return true;
}
HttpServletRequest
和HttpServletResponse
不再需要了。只是一个简单的服务。通常,您不希望您的服务层依赖于Web,因为这是您在解决方案中有效执行的操作。
答案 3 :(得分:0)
所以这就是我用注释解决它的方法。非常感谢M. Deinum!
我终于完成了所有工作。我没有使用XML。我使用Spring Boot但是如果我想配置一些东西,那就是我的配置方法。对于像我一样迷失的每个人,这是我在@Configuration类中的templateEngine配置:
@Bean
public ClassLoaderTemplateResolver emailTemplateResolver(){
ClassLoaderTemplateResolver emailTemplateResolver = new
ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("/WEB-INF/emails/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode("HTML5");
emailTemplateResolver.setCharacterEncoding("UTF-8");
emailTemplateResolver.setOrder(1);
return emailTemplateResolver;
}
@Bean
public TemplateEngine templateEngine(){
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(emailTemplateResolver());
return templateEngine;
}
通过这种配置我可以@Autowire
private TemplateEngine templateEngine;
在我的服务中,现在我不需要请求,回复。一切都适用于Context