欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring事件处理 博客分类: spring spring事件处理 

程序员文章站 2024-03-25 20:43:58
...

 

spring中已经内置的几种事件:

ContextClosedEvent   、ContextRefreshedEvent  、ContextStartedEvent  、ContextStoppedEvent   、RequestHandleEvent

 

首先自定义 2 种事件:

 

package com.tch.test.ssh.spring.event;

import org.springframework.context.ApplicationEvent;
/**
 * 降价通知
 * @author Administrator
 *
 */
public class PriceDecrease extends ApplicationEvent {

	private static final long serialVersionUID = 1L;

	public PriceDecrease(Object source) {
		super(source);
	}
	public PriceDecrease(String description) {
		super(description);
	}

}

 

package com.tch.test.ssh.spring.event;

import org.springframework.context.ApplicationEvent;
/**
 * 涨价通知
 * @author Administrator
 *
 */
public class PriceIncrease extends ApplicationEvent {

	private static final long serialVersionUID = 1L;

	public PriceIncrease(Object source) {
		super(source);
	}
	public PriceIncrease(String description) {
		super(description);
	}

}

 

 

然后定义监听器,并交给spring管理:

 

package com.tch.test.ssh.spring.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.tch.test.ssh.spring.event.PriceDecrease;
import com.tch.test.ssh.spring.event.PriceIncrease;
/**
 * 价格监听器
 * @author Administrator
 *
 */
@Component
public class PriceListener implements ApplicationListener {

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if(event instanceof PriceDecrease){
			System.out.println("PriceListener监听到PriceDecrease事件:"+event.getSource());
		}else if(event instanceof PriceIncrease){
			System.out.println("PriceListener监听到PriceIncrease事件:"+event.getSource());
		}
	}

}

 

 

接下来定义发布事件的组件(可以使用下面两种方法之一):

1:实现ApplicationEventPublisherAware 接口

 

package com.tch.test.ssh.spring.event;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
@Component
public class EventPublisher implements ApplicationEventPublisherAware {

	private static ApplicationEventPublisher eventPublisher;
	
	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
		System.out.println("注入属性:ApplicationEventPublisher");
		EventPublisher.eventPublisher = eventPublisher;
	}

	public static void publishEvent(ApplicationEvent  event){
		System.out.println("EventPublisher发布了一个事件");
		eventPublisher.publishEvent(event);
	}
	
}

 

 

2:实现ApplicationContextAware 接口:

package com.tch.test.ssh.spring.event;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;
@Component
public class GlobalEventPublisher implements ApplicationContextAware {

	private static ApplicationContext context;
	
	@Override
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		GlobalEventPublisher.context = context;
	}

	public static void publishApplicationEvent(ApplicationEvent event){
		System.out.println("通过GlobalEventPublisher发布了一个事件");
		context.publishEvent(event);
	}
	
}

 

下面是spring的配置文件:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
     <context:annotation-config></context:annotation-config>
     <context:component-scan base-package="com.tch.test.ssh.spring"></context:component-scan>
</beans> 

 

 

最后测试:

 

package test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tch.test.ssh.spring.event.EventPublisher;
import com.tch.test.ssh.spring.event.GlobalEventPublisher;
import com.tch.test.ssh.spring.event.PriceDecrease;
import com.tch.test.ssh.spring.event.PriceIncrease;

public class TestSpring {

	public static void main(String[] args) throws Exception {
		new ClassPathXmlApplicationContext("applicationContext.xml");
		PriceDecrease decrease = new PriceDecrease("降价通知");
		PriceIncrease increase  = new PriceIncrease("涨价通知");
		EventPublisher.publishEvent(decrease);
		GlobalEventPublisher.publishApplicationEvent(increase);
	}
	
}

 

输出结果:

 

EventPublisher发布了一个事件

PriceListener监听到PriceDecrease事件:降价通知

通过GlobalEventPublisher发布了一个事件

PriceListener监听到PriceIncrease事件:涨价通知

 

 

 

 

 

 

 

 

相关标签: spring 事件处理