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