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/secondSpringEmailTestCon
public class SecondSpringEmailAppConf
}
这个配置文件与原来的spring的xml样式的配置文件功能相同,不多解释。现在spring的annotation并不是很成熟,我们可以将一些不方便用annotation的部分放到一个xml中配置,然后用@ImportResource("classpath:/com/guan/chapter19/email/secondSpringEmailTestCon
这个xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
</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
}
这个类不多解释,因为如果您对spring发送邮件熟悉的话,很容易看懂,如果您不懂情参见我的关于spring email的日志。
最后写的是测试程序:
package com.guan.chapter19.email;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicat
public class SecondEmailTest {
}
要注意,这里的工厂类不是传入xml文件了,而是传入了一个配置类。
需要的文件包如下:
Spring 发送邮件需要的:
activation.jar
mail.jar
Org.springframework.context.support-3.0.3.RELEASE.jar
Org.springframework.core-3.0.3.RELEASE.jar
使用spring annotation进行spring配置需要下面的包:
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
### 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 DriverManagerConnectionP
#log4j.logger.org.hibernate.connection.DriverManagerConnectionP