spring 使用annotation替代xml配置实例(spring实现mail简单实现)
2014-10-27 14:41
816 查看
pring的配置类,这个是spring3.0的新特性,主要是想将以前的xml形式的配置模式转换成这种标识模式,相关内容可参考spring官方文档\spring-framework-3.0.3.RELEASE\docs\spring-framework-reference\html\beans.html的后面关于@Configuration那部分内容:
package com.guan.chapter19.email;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
@ImportResource("classpath:/com/guan/chapter19/email/secondSpringEmailTestConfig.xml")
public class SecondSpringEmailAppConfig
{
private @Value("${email.host}") String emailHost;
private @Value("${email.username}") String userName;
private @Value("${email.password}") String password;
private @Value("${email.from}") String from;
private @Value("${email.to}") String to;
private @Value("${mail.smtp.auth}") String mailAuth;
public @Bean MailSender
mailSender(){
JavaMailSenderImpl
ms = new JavaMailSenderImpl();
ms.setHost(emailHost);
ms.setUsername(userName);
ms.setPassword(password);
Properties
pp = new Properties();
pp.setProperty("mail.smtp.auth", mailAuth);
ms.setJavaMailProperties(pp);
return ms;
}
public @Bean SimpleMailMessage
mailMessage(){
SimpleMailMessage
sm = new SimpleMailMessage();
sm.setFrom(from);
sm.setTo(to);
return sm;
}
public @Bean SecondSpringEmailService secondSpringEmailService()
{
return new SecondSpringEmailService();
}
}
这个配置文件与原来的spring的xml样式的配置文件功能相同,不多解释。现在spring的annotation并不是很成熟,我们可以将一些不方便用annotation的部分放到一个xml中配置,然后用@ImportResource("classpath:/com/guan/chapter19/email/secondSpringEmailTestConfig.xml")来引入。
这个xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholderlocation="classpath:/com/guan/chapter19/email/email.properties"/>
</beans>
实际上,这个配置文件,仅仅加载了一个属性文件,属性文件的内容如下:
email.host=smtp.163.com
email.username=woshiguanxinquan
email.password=***********
email.from=woshiguanxinquan@163.com
email.to=woshiguanxinquan@163.com
mail.smtp.auth=true
属性文件包含了一个对系统的基本配置(最长见的情况是配置数据库,因为我正在学习spring的email功能,所以这里写了个发送邮件的属性);
下面是发送邮件的类的具体内容:
package com.guan.chapter19.email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;
@Component
public class SecondSpringEmailService {
private MailSender emailSender;
private SimpleMailMessage mailMessage;
public void sendAMessage(String
subject,String text)
{
mailMessage.setText(text);
mailMessage.setSubject(subject);
emailSender.send(mailMessage);
}
public SimpleMailMessage
getMailMessage() {
return mailMessage;
}
@Autowired
public void setMailMessage(SimpleMailMessage
mailMessage) {
this.mailMessage =
mailMessage;
}
public MailSender
getEmailSender() {
return emailSender;
}
@Autowired
public void setEmailSender(MailSender
emailSender) {
this.emailSender =
emailSender;
}
}
这个类不多解释,因为如果您对spring发送邮件熟悉的话,很容易看懂,如果您不懂情参见我的关于spring
email的日志。
最后写的是测试程序:
package com.guan.chapter19.email;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SecondEmailTest
{
@Test
public void secondMailTest()
{
ApplicationContext
ac = new AnnotationConfigApplicationContext(SecondSpringEmailAppConfig.class);
SecondSpringEmailService sses
= ac.getBean(SecondSpringEmailService.class);
sses.sendAMessage("da
guan ni hao", "这是spring发送email的测试程序");
}
}
要注意,这里的工厂类不是传入xml文件了,而是传入了一个配置类。
需要的文件包如下:
Spring 发送邮件需要的:
Spring没有提供的需要自己下的: sun与发送邮件相关的
activation.jar
mail.jar
Spring 3.0提供的:
Org.springframework.beans-3.0.3.RELEASE.jar 这三个是spring必须的
Org.springframework.context.support-3.0.3.RELEASE.jar
Org.springframework.core-3.0.3.RELEASE.jar
Spring 2.5 提供的:
Velocity-1.5.jar email可以使用模板
Velocity-tools-view-1.4.jar
使用spring annotation进行spring配置需要下面的包:
Spring没有提供的,需要自己下的:
Cglib-nodep-2.2.jar @configuration必须有这个包才能运行
Commons-logging-1.1.1.jar email的错误处理需要日志输出
Dom4j.jar
Log4j-1.2.16.jar
Spring 3.0提供的:
Org.springframework.aop-3.0.3.RELEASE.jar 切面编程,自身并没有做错误处理
Org.springframework.asm-3.0.3.RELEASE.jar
Org.springframework.context-3.0.3.RELEASE.jar
Org.springframework.expression-3.0.3.RELEASE.jar 使用了EL表达式@Value
Spring2.5 提供的:
Asm-2.2.3.jar
最后是junit测试:
Junit-4.7.jar
Log4j.properties:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
package com.guan.chapter19.email;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
@ImportResource("classpath:/com/guan/chapter19/email/secondSpringEmailTestConfig.xml")
public class SecondSpringEmailAppConfig
{
private @Value("${email.host}") String emailHost;
private @Value("${email.username}") String userName;
private @Value("${email.password}") String password;
private @Value("${email.from}") String from;
private @Value("${email.to}") String to;
private @Value("${mail.smtp.auth}") String mailAuth;
public @Bean MailSender
mailSender(){
JavaMailSenderImpl
ms = new JavaMailSenderImpl();
ms.setHost(emailHost);
ms.setUsername(userName);
ms.setPassword(password);
Properties
pp = new Properties();
pp.setProperty("mail.smtp.auth", mailAuth);
ms.setJavaMailProperties(pp);
return ms;
}
public @Bean SimpleMailMessage
mailMessage(){
SimpleMailMessage
sm = new SimpleMailMessage();
sm.setFrom(from);
sm.setTo(to);
return sm;
}
public @Bean SecondSpringEmailService secondSpringEmailService()
{
return new SecondSpringEmailService();
}
}
这个配置文件与原来的spring的xml样式的配置文件功能相同,不多解释。现在spring的annotation并不是很成熟,我们可以将一些不方便用annotation的部分放到一个xml中配置,然后用@ImportResource("classpath:/com/guan/chapter19/email/secondSpringEmailTestConfig.xml")来引入。
这个xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholderlocation="classpath:/com/guan/chapter19/email/email.properties"/>
</beans>
实际上,这个配置文件,仅仅加载了一个属性文件,属性文件的内容如下:
email.host=smtp.163.com
email.username=woshiguanxinquan
email.password=***********
email.from=woshiguanxinquan@163.com
email.to=woshiguanxinquan@163.com
mail.smtp.auth=true
属性文件包含了一个对系统的基本配置(最长见的情况是配置数据库,因为我正在学习spring的email功能,所以这里写了个发送邮件的属性);
下面是发送邮件的类的具体内容:
package com.guan.chapter19.email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;
@Component
public class SecondSpringEmailService {
private MailSender emailSender;
private SimpleMailMessage mailMessage;
public void sendAMessage(String
subject,String text)
{
mailMessage.setText(text);
mailMessage.setSubject(subject);
emailSender.send(mailMessage);
}
public SimpleMailMessage
getMailMessage() {
return mailMessage;
}
@Autowired
public void setMailMessage(SimpleMailMessage
mailMessage) {
this.mailMessage =
mailMessage;
}
public MailSender
getEmailSender() {
return emailSender;
}
@Autowired
public void setEmailSender(MailSender
emailSender) {
this.emailSender =
emailSender;
}
}
这个类不多解释,因为如果您对spring发送邮件熟悉的话,很容易看懂,如果您不懂情参见我的关于spring
email的日志。
最后写的是测试程序:
package com.guan.chapter19.email;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SecondEmailTest
{
@Test
public void secondMailTest()
{
ApplicationContext
ac = new AnnotationConfigApplicationContext(SecondSpringEmailAppConfig.class);
SecondSpringEmailService sses
= ac.getBean(SecondSpringEmailService.class);
sses.sendAMessage("da
guan ni hao", "这是spring发送email的测试程序");
}
}
要注意,这里的工厂类不是传入xml文件了,而是传入了一个配置类。
需要的文件包如下:
Spring 发送邮件需要的:
Spring没有提供的需要自己下的: sun与发送邮件相关的
activation.jar
mail.jar
Spring 3.0提供的:
Org.springframework.beans-3.0.3.RELEASE.jar 这三个是spring必须的
Org.springframework.context.support-3.0.3.RELEASE.jar
Org.springframework.core-3.0.3.RELEASE.jar
Spring 2.5 提供的:
Velocity-1.5.jar email可以使用模板
Velocity-tools-view-1.4.jar
使用spring annotation进行spring配置需要下面的包:
Spring没有提供的,需要自己下的:
Cglib-nodep-2.2.jar @configuration必须有这个包才能运行
Commons-logging-1.1.1.jar email的错误处理需要日志输出
Dom4j.jar
Log4j-1.2.16.jar
Spring 3.0提供的:
Org.springframework.aop-3.0.3.RELEASE.jar 切面编程,自身并没有做错误处理
Org.springframework.asm-3.0.3.RELEASE.jar
Org.springframework.context-3.0.3.RELEASE.jar
Org.springframework.expression-3.0.3.RELEASE.jar 使用了EL表达式@Value
Spring2.5 提供的:
Asm-2.2.3.jar
最后是junit测试:
Junit-4.7.jar
Log4j.properties:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
相关文章推荐
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现
- Spring的Ioc简单使用实例(含XML配置文件路径知识)
- Spring 注解(Annotation)代替XML实现零配置
- Spring依赖注入的XML配置文件的实现思想(2)——简单的实现
- Spring进阶之路(11)-使用Aspectj切面配置和XML配置文件方式实现切面编程
- Spring的“零配置”支持,用annotation替代xml
- 学习新事物:使用jquery+xml实现ajax简单实例
- Spring学习笔记 使用XML配置实现Bean的auto-wiring (自动绑定)
- Spring_使用XML配置文件实现依赖注入
- Spring使用AspectJ注解和XML配置实现AOP
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- 关于使用Spring和hibernate开发web程序的配置说明和简单实例的详细说明
- SpringAOP实例之简单实现(Annotation形式)
- SpringMVC配置之使用annotation替代xml
- 在web.xml中配置,使用配置类替代springmvc.xml和applicationContext.xml
- 使用XPath解析xml实现简单的Spring IOC完成bean的依赖注入
- spring中配置sessionfactory使xml和annotation可以同时使用
- spring 使用xml配置文件实现quartz作业调度
- 使用Spring的配置xml方式实现AOP
- spring 框架中的依赖注入(IOC--设值注入)--使用注解--的具体实例的简单实现