`
dreamoftch
  • 浏览: 492814 次
  • 性别: 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事件处理基于发布/订阅模式,其中事件是消息的载体,而事件监听器则是对这些消息感兴趣的订阅者。在这个场景中,我们将探讨Spring MVC实例与Spring事件处理的结合。 首先,让我们了解Spring MVC。Spring MVC...

    spring事件的例子

    下面我们将深入探讨Spring事件处理的相关知识点。 首先,Spring事件是通过`ApplicationEvent`类来表示的。这是一个抽象基类,用于定义所有自定义事件。例如,如果你有一个用户注册的服务,你可能会创建一个`...

    JAVA-spring学习资源之spring事件

    Spring事件处理提供了多种方式来定制事件处理行为。比如,你可以使用`@EventListener`注解来监听事件,这样可以更直接地将事件处理绑定到特定的方法上,同时可以配置一些额外的属性,如`condition`用于条件判断,`...

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

    以下将详细介绍Spring事件处理的核心概念、工作流程以及如何在实践中应用。 首先,Spring通过`ApplicationContextAware`接口提供了一种方式,使得Bean能够获取到ApplicationContext的引用。当一个Bean实现了这个...

    详解Spring事件驱动模型

    在本文中,我们将深入探讨Spring事件驱动模型,理解其工作原理,并通过源码分析来深化认识。 首先,Spring事件驱动模型是基于观察者模式实现的,即发布者(Publisher)发布事件,订阅者(Subscriber)监听并处理...

    使用Spring事件机制实现异步的方法

    使用Spring事件机制实现异步的方法 Spring框架提供了一种事件机制,允许开发者将代码解耦合,实现松散耦合的设计理念。这里将详细介绍如何使用Spring事件机制实现异步的方法。 事件机制的实现主要包括三个部分:...

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

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

    Spring 学习2

    总的来说,Spring 事件处理机制是 Spring 框架提供的一种强大的组件间通信手段,允许开发者在不直接依赖的情况下传递信息。通过定义自定义事件、监听器和事件发布,我们可以构建出松散耦合的系统,使得系统更加灵活...

    Spring事件管理

    接下来,我们将深入探讨Spring事件管理的核心概念和实现方式。 首先,Spring事件是基于Java的观察者模式(Observer Pattern)实现的,它定义了主题(Subject)和观察者(Observer)之间的关系,当主题的状态发生...

    spring 事件监听 3种方式

    本篇文章将详细介绍Spring事件监听的三种方式:基于接口的监听器、基于类的监听器以及基于注解的监听器。 ### 1. 基于接口的监听器(ApplicationListener) **接口定义:** Spring提供了`ApplicationListener`接口...

    Spring4.3.zip

    5. **类型安全的事件处理**:Spring事件处理机制现在支持类型安全的事件发布和订阅,减少了类型转换错误的可能性,提高了代码质量。 6. **Web MVC改进**:Spring MVC在4.3中引入了更多的注解,如`@ControllerAdvice...

    Spring的配置以及事件注入

    Spring提供了ApplicationEvent和ApplicationListener接口来实现事件处理。当你有一个事件需要在整个应用中传播时,可以创建一个自定义的事件类,继承自ApplicationEvent。例如: ```java public class CustomEvent ...

    spring的Applicationcontext对事件的监听,实现类似MQ的效果

    首先,`ApplicationContext`的事件处理机制是基于Java的`java.util.EventObject`类的。Spring定义了一个名为`ApplicationEvent`的抽象类,它是所有应用事件的基础。当你有一个需要传播的消息或事件时,你可以创建一...

    spring-batch分区处理示例

    5. **监听器和事件**:Spring Batch允许添加监听器来跟踪分区过程中的事件,如PartitionStepExecutionListener,以便在子任务开始、结束时记录日志或执行其他操作。 6. **配置文件**:通常,这些配置会在Spring ...

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

    - 事件监听器可以是任何Spring管理的bean,这样可以轻松地将事件处理逻辑融入到现有应用中。 - 自定义事件可以扩展Spring的功能,满足复杂业务场景的需求。 需要注意的是,Spring事件监听机制并不适合所有的交互...

    spring事件机制

    Spring框架的事件机制是其核心特性之一,它提供了一种基于发布-订阅模式的事件处理方式,使得在Spring应用中的不同组件之间可以进行解耦通信。这个机制允许一个组件(通常是一个服务)触发一个事件,然后其他感兴趣...

    event.rar-Spring事件监听机制

    下面我们将详细探讨Spring事件监听机制的相关知识点。 1. **什么是Spring事件** Spring事件(ApplicationEvent)是Spring框架内建的一种轻量级消息传递机制。它基于Java的观察者模式,其中事件是被观察的对象,而...

    第十七章 Spring 事件(Events)1

    在Spring框架中,事件处理是一种重要的通信机制,它允许组件之间进行解耦的通信。本章将深入探讨Spring事件(Events)的相关知识点。 首先,Java事件/监听器编程模型是基于观察者模式的,该模式定义了对象之间的一...

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

    EventBus和Spring Event都是基于事件机制的异步处理方式,但它们有着不同的设计理念和实现方式。在本文中,我们将详细剖析EventBus和Spring Event的区别,帮助读者更好地理解和选择合适的事件机制。 EventBus事件...

Global site tag (gtag.js) - Google Analytics