閱讀288 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Spring-事件傳遞



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"
"https://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--定義一個Bean-->
 
    
    <bean  >
    
    </bean>
    

    <bean  />
    
    <bean  />
    
    <bean  />


</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]





最後更新:2017-04-02 22:16:38

  上一篇:go 軟件框架設計之美
  下一篇:go Spring-管理Bean 使用BeanFactory管理Bean