- 浏览: 360011 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
呆呆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 3497闲来无事翻了下以前写的项目,发现一个还算不错的东西 ... -
关于OpenSessionInViewFilter
2008-03-04 16:05 1514用了OpenSessionInViewFilter之后,程序开 ... -
Spring 事务简化配置
2008-03-02 19:44 1226在 spring 中, 事务管理一般是通过声明一个 txPro ... -
详解spring2.0的scope
2008-02-25 19:14 9564如何使用spring的作用域: <bean id=& ... -
扩展Spring——外部属性文件安全(三)
2008-02-17 16:13 1994要完成属性文件的加密工作,首先,必须获取一个密钥文件, ... -
扩展Spring——外部属性文件安全(二)
2008-02-17 16:11 2447编写支持加密属性文件的实现类 通过以上分析,我们设计 ... -
扩展Spring——外部属性文件安全(一)
2008-02-17 16:02 4188前言 在Spring的开发中, ... -
Spring 中与 Aware相关的接口
2008-02-17 15:49 3897Spring中提供一些Aware相关接口,像是BeanFact ... -
用 Spring 发送邮件的注意事项
2008-01-23 19:32 2821使用Spring 的封装的MailSender,确实省事多了! ... -
利用Spring简单使用quartz实现定时作业
2008-01-18 00:47 1551定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间进行 ... -
利用Spring按调度计划调用方法
2008-01-18 00:39 1974为了调度报表邮件,你不得不编写EmailReportJob B ... -
在Spring中使用Quartz调度器
2008-01-18 00:32 9227Quartz调度器为调度工作提供了更丰富的支持。和Java定时 ... -
在Spring中使用 Java Timer 调度任务
2008-01-18 00:10 5761从Java 1.3开始,Java SDK就通过java.uti ... -
Spring 中关于文件上传与 MultipartResolver
2008-01-16 14:16 12875在 Spring 中, MultipartResolver 主 ... -
spring mvc的异常处理
2008-01-16 14:06 5192在 Spring 中,框架自动集成了异常处理,其主要核心是由 ... -
Spring2.0 声明式事务配置
2008-01-11 16:14 2790<!-- 声明一个事务管理器 --> <b ...
相关推荐
标题“spring-velocity-support-2.3”暗示了一个关于Spring框架对Velocity模板引擎支持的特定版本,即2.3版。这个版本可能包含了用于整合Spring和Velocity的各种组件、类库和配置,使得开发者能够利用Velocity的强大...
Velocity入门教程,语法,Velocity布局,Spring框架集成Velocity
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.spring...
根据提供的信息,我们可以详细探讨如何使用Spring框架结合Velocity模板引擎来实现邮件的自动化发送功能。这一过程涉及到Spring框架的基本配置、Velocity模板引擎的使用以及JavaMail API的应用。 ### Spring框架与...
总的来说,Spring MVC与Velocity的整合使得开发者可以利用Spring MVC的强大功能来处理业务逻辑,同时利用Velocity的简洁模板语法来设计前端页面,两者结合提供了高效且易于维护的Web应用解决方案。
Spring Velocity是一个将Velocity模板引擎与Spring框架集成的示例,这个例子主要展示了如何在Spring应用中使用Velocity来渲染动态内容。Velocity是一个Java模板引擎,它允许开发者将逻辑和表示分离,使得网页设计...
2.导入本项目中的org.springframework目录下的velocity支持类,原样copy过去或自己打个jar 3.不想写java类配置就直接@ImportResource({"classpath:velocity.xml"})原来的xml配置 4.其他原来是啥就是啥开工
标题“spring+velocity+ibatis”揭示了一个基于Java的Web应用程序开发组合,它结合了Spring框架、Velocity模板引擎和iBatis数据访问层。这个项目可能是为了演示或教学如何有效地集成这三个组件,以便构建一个完整的...
Spring MVC、MyBatis 和 Velocity 是三个在Java Web开发中广泛应用的开源框架。Spring MVC 是Spring框架的一部分,用于构建高效、灵活的Web应用程序。MyBatis 是一个优秀的持久层框架,它支持定制化SQL、存储过程...
在这个项目中,"velocity spring jpa hibernate 整合"涉及到的是四个关键组件:Velocity、Spring、JPA(Java Persistence API)和Hibernate。让我们逐一深入理解这些技术并探讨它们如何协同工作。 1. **Velocity**...
Spring Mail 是一个在Java应用程序中发送电子邮件的库,它与流行的Spring框架紧密集成,使得邮件发送功能的实现变得更加简单和灵活。Spring Mail 提供了一种优雅的方式来配置和使用JavaMail API,而无需直接处理复杂...
Struts、Spring 和 Velocity 是Java开发中常用的三个框架,它们在构建企业级Web应用程序时起着关键作用。这里我们将深入探讨这些技术的核心概念、如何协同工作以及它们在实际项目中的应用。 **Struts** Struts 是一...
Spring、SpringMVC、Mybatis、Velocity和Maven是Java Web开发中常用的一组技术栈,它们各自在软件开发的不同层面发挥着重要作用。这个压缩包文件的标题和描述表明,它提供了一个集成这些技术的演示项目,下面我们将...
Spring Velocity Demo是一个示例项目,展示了如何在Spring框架中集成Velocity模板引擎,以实现动态页面渲染。这个项目基于Eclipse IDE构建,利用了Maven作为依赖管理工具,使得开发环境的搭建更为便捷。以下是关于...
Struts2、Spring和Velocity是Java Web开发中的三个重要框架,它们共同构建了一个高效、灵活且可扩展的应用程序架构。让我们深入探讨这三个组件以及如何在项目中整合它们。 **Struts2** 是一个基于MVC(Model-View-...
分享一个spring+mybatis+velocity项目demo,该项目是之前给一个学第学习用的,主要基于springMVC、mybatis、velocity搭建的,使用maven构建,其中zai service层编写了两个简单组件,一个是email发送,一个是认证授权...
Spring Mail支持创建复杂的HTML邮件,可以利用Velocity或FreeMarker模板引擎来生成动态内容。 6. **邮件附件** 发送带有附件的邮件也是常见的需求。Spring Mail提供了`MimeMessageHelper`类,允许我们方便地添加...
在Java Web开发中,Spring框架和Velocity模板引擎都是常见的工具。Spring主要用于依赖注入、AOP(面向切面编程)以及企业级应用的构建,而Velocity则是一个轻量级的模板引擎,专注于视图层的渲染,提供简洁的语法来...