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

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

分析


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();
    }
}

运行结果

image.png

项目链接—具体包

目录
相关文章
|
1月前
|
存储 Java 数据安全/隐私保护
|
1月前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
27 1
|
1天前
|
消息中间件 Java Kafka
集成Kafka到Spring Boot项目中的步骤和配置
集成Kafka到Spring Boot项目中的步骤和配置
21 7
|
1天前
|
druid Java 关系型数据库
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
16 5
|
3天前
|
XML Java 数据格式
Spring Boot自动配置是通过`@EnableAutoConfiguration`注解启用的
【6月更文挑战第18天】Spring Boot的`@EnableAutoConfiguration`启动自动配置,基于类路径扫描和条件注解(如@ConditionalOnClass)选择性应用配置。当检测到特定依赖时,自动配置模块会将对应的bean添加到应用上下文,简化了XML或Java配置。只需添加依赖,即可自动配置功能。
12 4
|
4天前
|
安全 Java 数据安全/隐私保护
Spring Security 6.x 一文快速搞懂配置原理
本文主要对整个Spring Security配置过程做一定的剖析,希望可以对学习Spring Sercurity框架的同学所有帮助。
30 5
|
13天前
|
存储 消息中间件 Java
Java一分钟之-Spring Cloud Config:外部化配置
【6月更文挑战第8天】Spring Cloud Config提供外部化配置,通过Config Server管理和版本控制微服务配置。本文涵盖Config Server与Client的配置、常见错误、多环境配置、实时更新及使用示例。注意配置服务器URL、环境变量设置、Bus配置以及安全问题。使用Config能提升系统灵活性和可维护性,但要留意日志以确保配置正确和安全。
90 10
|
7天前
|
缓存 Java Maven
Spring Boot 启动错误:To display the conditions report re-run your application with ‘debug‘ enable —【已解决】
Spring Boot 启动错误:To display the conditions report re-run your application with ‘debug‘ enable —【已解决】
15 1
|
10天前
|
Java Serverless 应用服务中间件
Serverless 应用引擎产品使用合集之Web函数启动的Spring Boot项目可以通过什么方式配置Nginx
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
|
13天前
|
前端开发 Java 关系型数据库
在Spring3 MVC中五步配置集成注解方式Hibernate3
在Spring3 MVC中五步配置集成注解方式Hibernate3
20 3