ApplicationEvent类和ApplicationListener接口来提供,通过ApplicationContext的publishEvent()方法来通知ApplicationListener
LogEvent
package com.gc.action; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; public class LogEvent extends ApplicationEvent{ /** * */ private static final long serialVersionUID = 1L; public LogEvent(Object msg) { super(msg); // TODO Auto-generated constructor stub } }
LogListener
package com.gc.action; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class LogListener implements ApplicationListener{ @Override public void onApplicationEvent(ApplicationEvent event) { // TODO Auto-generated method stub if(event instanceof LogEvent) { //设定时间 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); format.setLenient(false); String currentDate=format.format(new Date()); System.out.println("输出时间:"+currentDate+"输出内容:"+event.toString()); } } }
LOG
package com.gc.action; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class Log implements ApplicationContextAware{ //设定变量applicationContext private ApplicationContext applicationContext; //变量applicationContext的set方法 @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.applicationContext=applicationContext; } //通过publishEvent发布时间 public int log(String log) { LogEvent event=new LogEvent(log);; this.applicationContext.publishEvent(event); return 0; } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!--定义一个Bean--> <bean id="HelloWorld" class="com.gc.action.HelloWorld"> </bean> <bean id="date" class="java.util.Date"/> <bean id="log" class="com.gc.action.Log"/> <bean id="listener" class="com.gc.action.LogListener"/> </beans>
测试程序:
package com.gc.test; import java.util.Date; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import com.gc.action.HelloWorld; import com.gc.action.Log; public class TestHelloWorld { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml"); Log log=(Log)actx.getBean("log"); log.log("gf"); //拿出Bean在配置文档中设定的内容 //System.out.println(helloWorld.getDate()+" "+helloWorld.getMsg()+"------"); } }
输出:
输出时间:2012-03-20 20:38:05输出内容:com.gc.action.LogEvent[source=gf]