【Spring】Spring常用配置-事件(Application Event)

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

分析

Spring的事件(Application Event)为Bean与Bean之间的消息通信提供了支持。
当一个Bean处理完一个任务之后,希望另一个Bean知道并能做出相应的处理,这时我们就需要让另外一个Bean监听当前Bean所发送的事件。

Spring的事件需要遵循如下流程:
1、自定义事件,集成ApplicationEvent。
2、定义事件监听器,实现ApplicationListener
3、使用容器发布容器

进行本示例的演示,需要先配置好Maven和Spring哦、
见:
【Spring】基于IntelliJ IDEA搭建Maven

下面直接上示例吧。

示例

自定义事件

package cn.hncu.p2_5_2ApplicationEvent;

import org.springframework.context.ApplicationEvent;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/15.
 * Time: 下午 8:39.
 * Explain:自定义事件
 */
public class DemoEvent extends ApplicationEvent{
    private static final long serialVersionUID = 1L;
    private String msg;

    public DemoEvent(Object source,String msg) {
        super(source);
        this.msg=msg;
        System.out.println(this.getClass()+",构造方法");
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

事件监听器

package cn.hncu.p2_5_2ApplicationEvent;

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

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/15.
 * Time: 下午 8:50.
 * Explain:事件监听器
 */
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {//实现ApplicationListener接口,并指定监听的事件类型
    @Override
    public void onApplicationEvent(DemoEvent demoEvent) {//使用onApplicationEvent方法对消息进行接受处理
        String msg = demoEvent.getMsg();
        System.out.println(this.getClass()+"监听到了bean-demoPublisher发布的消息:"+msg);
    }
}

事件发布类

package cn.hncu.p2_5_2ApplicationEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/15.
 * Time: 下午 10:39.
 * Explain:事件发布类
 */
@Component
public class DemoPublisher {
    @Autowired
    ApplicationContext applicationContext;//注入ApplicationContext用来发布事件

    public void publish(String msg){
        DemoEvent demoEvent = new DemoEvent(this,msg);
        applicationContext.publishEvent(demoEvent);//在这里的时候,会去运行DemoListener中的onApplicationEvent方法
        System.out.println("消息:"+demoEvent.getMsg());
        //使用ApplicationContext的publishEvent方法来发布
    }

}

配置类

package cn.hncu.p2_5_2ApplicationEvent;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/15.
 * Time: 下午 10:46.
 * Explain:配置类
 */
@Configuration
@ComponentScan("cn.hncu.p2_5_2ApplicationEvent")
public class EventConfig {
}

运行类

package cn.hncu.p2_5_2ApplicationEvent;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/15.
 * Time: 下午 10:47.
 * Explain:运行类
 */
public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
        DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
        demoPublisher.publish("hello 你好...");
        context.close();
    }
}

运行结果

项目链接—具体包

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

时间: 2025-01-29 17:57:28

【Spring】Spring常用配置-事件(Application Event)的相关文章

2.Spring常用配置—5.事件

1.点睛Spring的事件(Application Event)为Bean与Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要让另外一个Bean监听当前Bean所发送的事件.Spring的事件需要遵循如下流程:(1)自定义事件,继承ApplicationEvent(2)定义事件监听器,实现ApplicationListener(3)使用容器发布事件2.示例(1)自定义事件(2)事件监听器实现ApplicationListen

Spring常用配置

----------------------------------------------------------------------------------------------[版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/52471101作者:朱培      ID:sdksdk0      邮箱: zhupei@tianfang1314.cn   -------------------

《Spring Cloud Netflix》 -- 服务注册和服务发现-Eureka的常用配置

一.版本的说明 Angel版本对应Spring Boot 1.2.x,可以使用Spring Boot 1.3.x: Brixton版本对应Spring Boot 1.3.x,可以使用Spring Boot 1.4.x: Camden版本对应Spring Boot 1.4.x,可以使用Spring Boot 1.5.x: Dalston版本对应Spring Boot 1.5.x 二.应用进行热部署 添加依赖: <dependency> <groupId>org.springframe

2.Spring常用配置—1.Bean的Scope

1.点睛 Scope描述的是Spring容器如何新建Bean的实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只有一个Bean的实例,此为Spring的默认配置,全容器共享一个实例. (2)Prototype:每次调用新建一个Bean的实例. (3)Request:Web项目中,给每一个http request新建一个Bean实例. (4)Session:Web项目中,给每一个http session新建一个Bean实例.

【Spring】Spring常用配置-Bean的Scope

转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 分析 Scope(范围)描述的是Spring容器如何新建Bean的实例的.可以简单的理解成Bean的作用范围! Spring的Scope有以下的几种,可以通过@Scope注解来实现. 1.singleton:一个Spring容器中只有一个Bean的实例. 这是Spring的默认配置,也就是不写@Scope("singleton"),全容器共享一个实例. 2.prototy

Spring中ApplicationContext的事件机制

   ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理.如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发.     Spring的事件框架有如下两个重要的成员: ApplicationE

Spring的容器内部事件发布

文/杜琪(简书作者) 原文链接:http://www.jianshu.com/p/4f0ad3bb98f0 著作权归作者所有,转载请联系作者获得授权,并标注"简书作者". 自定义事件机制 给出自定义事件类型在某些应用场景下,我们希望关注特定功能的执行情况,这种功能的开始或者结束或者异常都可以看做一个事件,因此需要定义自己的事件类型. package com.javadu.event; import java.util.EventObject; public class MethodEx

Spring中如何配置DataSource数据源

在Spring框架中有如下3种获得DataSource对象的方法: 1.从JNDI获得DataSource. 2.从第三方的连接池获得DataSource. 3.使用DriverManagerDataSource获得DataSource. 一.从JNDI获得DataSource SpringJNDI数据源配置信息: <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean&qu

Spring中ApplicationContext的事件机制(二 内定事件)

在Spring中已经定义了五个标准事件,分别介绍如下: 1)ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件. 2)ContextClosedEvent:当ApplicationContext被关闭时触发该事件.容器被关闭时,其管理的所有 单例Bean都被销毁. 3)RequestHandleEvent:在Web应用中,当一个http请求(request)结束触发该事件. ContestStartedEvent:Spring2.5新增的