使用环境:MyEclipse6.0+JDK1.5+Spring 2.0 Core Libraries
1,一个Java Bean
package spring.main.message;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2,一个事件
package spring.main.message;
import org.springframework.context.ApplicationEvent;
public class RainEvent extends ApplicationEvent {
public RainEvent(Object source) {
super(source);
System.out.println("乌云密布、闪电、打雷,紧接着,下起了瓢泼大雨。");
}
}
3,一个事件监听器
package spring.main.message;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class RainListener implements ApplicationListener{
public void onApplicationEvent(ApplicationEvent arg0) {
if(arg0 instanceof RainEvent){
System.out.println("唐僧大喊:"+arg0.getSource()+"赶紧收衣服咧!");
}
}
}
4,二个资源文件:epan_en_US.properties
userlogin user {0} login at {1}
epan_zh_CN.properties
userlogin \u4f7f\u7528\u8005 {0} \u4e8e {1}\u767b\u5f55
这里说明下epan_zh_CN.properties的来源:
4.1建文件test.properties,内容为:userlogin 使用者 {0} 于 {1}登录;
4.2在命令下行进入test.properties所在目录,
4.3使用命令:native2ascii test.properties epan_zh_CN.properties。
4.4生成了epan_zh_CN.properties文件了。
5,配置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="user"
class="spring.main.message.User">
<property name="name">
<value>Epan</value>
</property>
<property name="age">
<value>24</value>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
abstract="false"
lazy-init="default"
autowire="default"
dependency-check="default">
<property name="basename" value="epan"></property>
</bean>
<bean id="listener"
class="spring.main.message.RainListener"></bean>
</beans>
6,一个测试
package spring.main.message;
import java.util.Calendar;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)context.getBean("user");
Object[] obj = new Object[]{
user.getName(),Calendar.getInstance().getTime()
};
System.out.println(context.getMessage("userlogin", obj,"can't find the file",Locale.CHINA));
System.out.println(context.getMessage("userlogin", obj,"can't find the file",Locale.US));
context.publishEvent(new RainEvent("it's raining!"));
}
}
7,建一个文件:log4j.properties
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
8,运行MainTest,输出如下结果:
使用者 Epan 于 08-10-14 上午10:29登录
user Epan login at 10/14/08 10:29 AM
乌云密布、闪电、打雷,紧接着,下起了瓢泼大雨。
唐僧大喊:it's raining!赶紧收衣服咧!
结构图: