- 浏览: 355958 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
呆呆DE萌萌:
不可以吗?Timer里不是有一个指定首次运行时间firstDa ...
在Spring中使用 Java Timer 调度任务 -
accpchf:
不太明白,Error jvm都已经停止线程了,怎么还能转译?
深入探索 高效的Java异常处理框架。 -
bo_hai:
讲的详细。谢谢!
详解spring2.0的scope -
hpjianhua:
学习下...
线程池的实现 -
eltonto:
以后你可以来这里看看favicon在线转换
Tomcat中使用Favicon
One of the requirements on my current project is to send Job Applicants an e-mail when they apply for a position. Since we're using Spring, I figured I'd try out its JavaMail and Velocity support to send this e-mail. Below is a short tutorial for setting up Spring's JavaMail support on a PositionManager class, followed by replacing the e-mail's text with a Velocity template. It's possible there's easier ways to do this, but this is what worked for me.
Step 1: Configure the JavaMailSenderImpl
The first step is to setup a MailSender for the PositionManager. To do this, you need to configure a JavaMailSenderImpl with a host or a session. For a host, it's rather simple. Add the following to your applicationContext.xml file:
Optionally, you can also configure it with a Session from JNDI when you're running in a servlet container:
I use the first option for JUnit tests, and the 2nd when running in Tomcat. Thanks to Juergen for showing me how easy the JNDI setup is. ;-)
Step 2: Configure a SimpleMailMessage with default values
Next you can configure a SimpleMailMessage with some default values in your applicationContext.xml file:
Step 3: Configure dependencies in PositionManagerImpl
Then in traditional Spring-style, you need to add variables and setters to the PositionManagerImpl class:
Then configure this class's definition in applicationContext.xml so Spring will inject its dependencies:
Now you should be able to easily send an e-mail in a method of this class:
The only problem with this is that the e-mail message is hard-coded into our Java code - so let's refactor it to use a Velocity template for the text.
Step 4: Configuring Velocity in applicationContext.xml
The next step is to use Spring's Velocity support classes to configure a VelocityEngine. For this, add the following to your applicationContext.xml file:
NOTE: You can also use <property name="configLocation">velocity.properties</property> if velocity.properties file is in your classpath. However, my velocity.properties file has a webapp.loader defined in it, and since this depends on javax.servlet.ServletContext, I didn't want to use it in my business logic layer. You could also load velocity.properties with PropertyPlaceholderConfigurer and then refer to ${class.resource.loader.class}.
Step 5: Configure Velocity dependency in PositionManagerImpl
In order to use this nice little velocityEngine you just configured, you'll need to add a variable and setter to PositionManagerImpl:
And configure it's dependency in applicationContext.xml:
Now you can refactor the text part of the previous e-mail sending logic to use a template.
Pretty slick huh? A further configuration option is to use Spring to set the name of the template. If you know of any better ways to do JavaMail and e-mail templates with Spring, or find errors in my code - please let me know.
One thing that seems to wrong with this is that when I run my PositionManagerTest JUnit test - it initializes Velocity a number of times. This is because the PersonManagerImpl is re-initialized each time in my setUp() method. This is a JUnit issue, not a Spring issue. I could probably do something so the PositionManagerImpl is only created once for the entire Test run. Either that, or figure out a way to initialize Velocity for only one test. Hints would be great.
Another issue is that I'd like to use Velocity's DataSourceResourceLoader, but it only accepts a JNDI DataSource name. It'd be nice if there was an alternative version that would allow setting of the DataSource via IoC.
Step 1: Configure the JavaMailSenderImpl
The first step is to setup a MailSender for the PositionManager. To do this, you need to configure a JavaMailSenderImpl with a host or a session. For a host, it's rather simple. Add the following to your applicationContext.xml file:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host"><value>localhost</value></property> <!-- If you don't want to hardcode "localhost", load it from a mail.properties file with PropertyPlaceholderConfigurer --> </bean>
Optionally, you can also configure it with a Session from JNDI when you're running in a servlet container:
<bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"><value>java:comp/env/mail/Session</value></property> </bean> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="session"><ref bean="mailSession"/></property> </bean>
I use the first option for JUnit tests, and the 2nd when running in Tomcat. Thanks to Juergen for showing me how easy the JNDI setup is. ;-)
Step 2: Configure a SimpleMailMessage with default values
Next you can configure a SimpleMailMessage with some default values in your applicationContext.xml file:
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from"><value><![CDATA[Human Resources <hr@raibledesigns.com>]]></value></property> <property name="subject"><value>Your application has been received</value></property> </bean>
Step 3: Configure dependencies in PositionManagerImpl
Then in traditional Spring-style, you need to add variables and setters to the PositionManagerImpl class:
private MailSender mailSender; private SimpleMailMessage message; public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public void setMessage(SimpleMailMessage message) { this.message = message; }
Then configure this class's definition in applicationContext.xml so Spring will inject its dependencies:
<bean id="positionManagerTarget" class="org.appfuse.service.PositionManagerImpl"> ... <property name="mailSender"><ref bean="mailSender"/></property> <property name="message"><ref bean="mailMessage"/></property> ... </bean>
Now you should be able to easily send an e-mail in a method of this class:
// user and position objects looked up... SimpleMailMessage msg = new SimpleMailMessage(this.message); msg.setTo(user.getFullName() + "<" + user.getEmail() + ">"); StringBuffer txt = new StringBuffer(); txt.append("Dear " + user.getFullName() + ",\n\n"); txt.append("Thank you for application for our "); txt.append(position.getName() + " position. You can check "); txt.append(" on the status of this position at the URL below.\n\n"); txt.append(" http://raibledesigns.com/positions/status.jsp\n\n"); // doesn't really exist ;-) txt.append("Sincerely, \n\nRaible Designs Human Resources"); msg.setText(txt.toString()); try { mailSender.send(msg); } catch (MailException ex) { log.error(ex.getMessage()); }
The only problem with this is that the e-mail message is hard-coded into our Java code - so let's refactor it to use a Velocity template for the text.
Step 4: Configuring Velocity in applicationContext.xml
The next step is to use Spring's Velocity support classes to configure a VelocityEngine. For this, add the following to your applicationContext.xml file:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="velocityProperties"> <props> <prop key="resource.loader">class</prop> <prop key="class.resource.loader.class"> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader </prop> </props> </property> </bean>
NOTE: You can also use <property name="configLocation">velocity.properties</property> if velocity.properties file is in your classpath. However, my velocity.properties file has a webapp.loader defined in it, and since this depends on javax.servlet.ServletContext, I didn't want to use it in my business logic layer. You could also load velocity.properties with PropertyPlaceholderConfigurer and then refer to ${class.resource.loader.class}.
Step 5: Configure Velocity dependency in PositionManagerImpl
In order to use this nice little velocityEngine you just configured, you'll need to add a variable and setter to PositionManagerImpl:
private VelocityEngine velocityEngine; public void setVelocityEngine(VelocityEngine velocityEngine) { this.velocityEngine = velocityEngine; }
And configure it's dependency in applicationContext.xml:
<bean id="positionManagerTarget" class="org.appfuse.service.PositionManagerImpl"> ... <property name="velocityEngine"><ref bean="velocityEngine"/></property> ... </bean>
Now you can refactor the text part of the previous e-mail sending logic to use a template.
Map model = new HashMap(); model.put("user", user); model.put("position", position); String result = null; try { // notificationTemplate.vm must be in your classpath result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "notificationTemplate.vm", model); } catch (VelocityException e) { e.printStackTrace(); } msg.setText(result);
Pretty slick huh? A further configuration option is to use Spring to set the name of the template. If you know of any better ways to do JavaMail and e-mail templates with Spring, or find errors in my code - please let me know.
One thing that seems to wrong with this is that when I run my PositionManagerTest JUnit test - it initializes Velocity a number of times. This is because the PersonManagerImpl is re-initialized each time in my setUp() method. This is a JUnit issue, not a Spring issue. I could probably do something so the PositionManagerImpl is only created once for the entire Test run. Either that, or figure out a way to initialize Velocity for only one test. Hints would be great.
Another issue is that I'd like to use Velocity's DataSourceResourceLoader, but it only accepts a JNDI DataSource name. It'd be nice if there was an alternative version that would allow setting of the DataSource via IoC.
发表评论
-
基于约定的Spring MVC扩展
2009-11-05 12:12 3415闲来无事翻了下以前写的项目,发现一个还算不错的东西 ... -
关于OpenSessionInViewFilter
2008-03-04 16:05 1474用了OpenSessionInViewFilter之后,程序开 ... -
Spring 事务简化配置
2008-03-02 19:44 1182在 spring 中, 事务管理一般是通过声明一个 txPro ... -
详解spring2.0的scope
2008-02-25 19:14 9497如何使用spring的作用域: <bean id=& ... -
扩展Spring——外部属性文件安全(三)
2008-02-17 16:13 1945要完成属性文件的加密工作,首先,必须获取一个密钥文件, ... -
扩展Spring——外部属性文件安全(二)
2008-02-17 16:11 2378编写支持加密属性文件的实现类 通过以上分析,我们设计 ... -
扩展Spring——外部属性文件安全(一)
2008-02-17 16:02 4128前言 在Spring的开发中, ... -
Spring 中与 Aware相关的接口
2008-02-17 15:49 3854Spring中提供一些Aware相关接口,像是BeanFact ... -
用 Spring 发送邮件的注意事项
2008-01-23 19:32 2782使用Spring 的封装的MailSender,确实省事多了! ... -
利用Spring简单使用quartz实现定时作业
2008-01-18 00:47 1513定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间进行 ... -
利用Spring按调度计划调用方法
2008-01-18 00:39 1932为了调度报表邮件,你不得不编写EmailReportJob B ... -
在Spring中使用Quartz调度器
2008-01-18 00:32 9152Quartz调度器为调度工作提供了更丰富的支持。和Java定时 ... -
在Spring中使用 Java Timer 调度任务
2008-01-18 00:10 5692从Java 1.3开始,Java SDK就通过java.uti ... -
Spring 中关于文件上传与 MultipartResolver
2008-01-16 14:16 12835在 Spring 中, MultipartResolver 主 ... -
spring mvc的异常处理
2008-01-16 14:06 5149在 Spring 中,框架自动集成了异常处理,其主要核心是由 ... -
Spring2.0 声明式事务配置
2008-01-11 16:14 2747<!-- 声明一个事务管理器 --> <b ...
相关推荐
spring-velocity-support-2.3
spring+velocity发送邮件,spring+velocity发送邮件
Velocity入门教程,语法,Velocity布局,Spring框架集成Velocity
spring mvc与velocity整合
spring+velocity+ibatis
velocity spring jpa hibernate 整合
spring mvc mybatis velocity 示范
Spring+SpringMVC+Mybatis+Velocity+Maven整合
2.导入本项目中的org.springframework目录下的velocity支持类,原样copy过去或自己打个jar 3.不想写java类配置就直接@ImportResource({"classpath:velocity.xml"})原来的xml配置 4.其他原来是啥就是啥开工
分享一个spring+mybatis+velocity项目demo,该项目是之前给一个学第学习用的,主要基于springMVC、mybatis、velocity搭建的,使用maven构建,其中zai service层编写了两个简单组件,一个是email发送,一个是认证授权...
java 采用struts2+spring+velocity模板项目。
采用struts+spring,以及手写封装类组装数据到vo模型,采用velocity模板渲染数据,采用spring的注解对action,server,dao层,
NULL 博文链接:https://xutao5641745.iteye.com/blog/1667997
shiro_spring_velocity的整合文档
NULL 博文链接:https://yanxiansheng.iteye.com/blog/1533419
Learn how to simplify mail sending with Spring and how to integrate JMS messaging into your application using Spring and ActiveMQ. For more information on the content of this book, check out the ...
struts2+spring+velocity简单的扩展实例,下载下来就能运行。后续将整合各项技术。敬请期待。
Velocity+Spring 2[1].0+Hibernate的Web应用开发框架,讲解如何使用velocity生成spring+hibernate的框架代码。