`
dreamoftch
  • 浏览: 485275 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring事件处理

阅读更多

 

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 事件处理

    spring的mvc实例和spring的时间处理demo

    spring事件驱动 + 策略模式应用

    技术: 1. spring事件驱动(ApplicationEventPublisher) 2. 策略模式处理事件 目的: 1. 通过event,代码逻辑异步处理 2. 通过策略模式,构建具体监听实现 3. 解耦 4. 容错(降低代码块错误风险)

    深入探索Spring事件监听机制:技术与应用

    除此之外,Spring也支持自定义事件,提供灵活性来处理特定业务需求。 事件监听器是对事件响应的处理器。它们可以通过实现ApplicationListener接口或使用@EventListener注解来定义。这些监听器关注特定类型的事件,...

    J2EE企业级项目开发-1期 08 Spring中事件处理的小技巧.doc

    J2EE企业级项目开发-1期 08 Spring中事件处理的小技巧.doc 学习资料 复习资料 教学资源

    SpringMVC学习知识

    SpringMVC理论知识点,包括spring自动装配、spring事件处理、依赖注入、spring事务管理、AOP代理、AOP (部分,并非某个方面所有知识)

    Spring Boot实战与原理分析视频课程包含14-18

    11 Spring Boot 事件监听27:53 --四种方式讲解如何配置事件监听 12 Spring Boot 扩展分析33:54 13 Spring Boot 补充讲解21:59 14 Spring Boot 运行流程分析31:48 15 Spring Boot Web(一)59:01 16 Spring Boot...

    EventBus与Spring Event区别详解(EventBus 事件机制,Spring Event事件机制)

    主要介绍了EventBus与Spring Event区别,需要的朋友可以参考下

    Spring文档

    Spring文档 确实不错 1.spring概述 2.Spring的体系结构 3.Spring入门案例 4.IOC容器 5.Spring中的事件处理 6.Spring AOP 7.Spring的事务管理

    Spring_Framework_ API_5.0.5 (CHM格式)

    响应式编程提供了另一种编程风格,专注于构建对事件做出响应的应用程序。 SpringFramework5 包含响应流(定义响应性API的语言中立尝试)和 Reactor(由Spring Pivotal团队提供的 Reactive Stream 的Java实现), 以...

    spring事件扩展

    机制通知异常监听器进行处理。在笔者的一个项目中,就曾经借助事件机制,较好的实现了当系统 异常时在监视终端上报警,同时发送报警SMS至管理员手机的功能。 */ public class LoginAction implements ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    3.8.2. 事件 3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 Resource 实现...

    Spring 2.0 开发参考手册

    3.8.2. 事件 3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 ...

    开源框架 Spring Gossip

    Spring 容器的存在,然而有时候 Bean 仍必须知道有关于 Spring 容器或自己的一些讯息,而另一方面,您可能必须让容器对 Bean 进行一些额外处理。 不使用XML定义档进行 Bean设置 Aware 相关介面 ...

    事件流处理微服务:使用Spring Cloud Stream和Spring State Machine创建事件驱动的微服务

    事件流处理微服务示例 在事件驱动的微服务体系结构中,域事件的概念对于每个服务的行为至关重要。 随着微服务架构的持续流行,诸如CQRS (命令查询责任隔离)与事件源相结合的流行做法在应用程序中变得越来越普遍。 ...

    Spring攻略(第二版 中文高清版).part1

    2.14 使用应用事件进行通信 93 2.14.1 问题 93 2.14.2 解决方案 93 2.14.3 工作原理 94 2.15 在Spring中注册属性编辑器 96 2.15.1 问题 96 2.15.2 解决方案 96 2.15.3 工作原理 97 2.16 创建自定义...

    Spring 5 英文文档全套.7z

    IoC容器,事件,资源,i18n,验证,数据绑定,类型转换,SpEL,AOP。 测试 模拟对象,TestContext框架,Spring MVC测试,WebTestClient。 资料存取 事务,DAO支持,JDBC,O / R映射,XML编组。 Web Servlet ...

    Spring中文帮助文档

    3.8.3. 事件 3.8.4. 底层资源的访问 3.8.5. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-...

    springMongodb参考文档中文版

    1.了解Spring 2.了解NoSQL和文档数据库 3.要求 4.其他帮助资源 4.1。支持 4.1.1。社区论坛 4.1.2。专业支持 4.2。发展之后 5.新&值得注意的 5.1。Spring Data MongoDB 2.1中的新特性 5.2。Spring Data MongoDB 2.0...

    spring chm文档

    3.8.2. 事件 3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 ...

    Spring API

    3.8.3. 事件 3.8.4. 底层资源的访问 3.8.5. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-...

Global site tag (gtag.js) - Google Analytics