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