Spring-事件传递

  1. 云栖社区>
  2. 博客列表>
  3. 正文

Spring-事件传递

广贤 2012-03-21 09:31:00 浏览251 评论0

摘要: ApplicationEvent类和ApplicationListener接口来提供,通过ApplicationContext的publishEvent()方法来通知ApplicationListenerLogEvent package com.



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]





用云栖社区APP,舒服~

【云栖快讯】新年大招!云栖社区为在读大学生/研究生准备了一份学(huan)习(zhuang)攻略,发布博文即有机会赢得iPad mini 4等大奖,学习换装两不误!欢迎报名参与~  详情请点击

网友评论

广贤
文章363篇 | 关注6
关注
基于全网公开发布数据、传播路径和受众群体画像,利用语义分析、情感算法和机器学习,分析公众对品... 查看详情
阿里云函数计算(Function Compute)是一个事件驱动的全托管计算服务。通过函数计... 查看详情
是众安保险针对阿里云用户推出的信息安全综合保险,若因黑客攻击导致用户云服务器上的数据泄露并造... 查看详情
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效... 查看详情
2017阿里千余份技术干货大盘点

2017阿里千余份技术干货大盘点