• Spring 中的事件处理


    Spring 中的事件处理

    Spring 的核心是 ApplicationContext,它负责管理 beans 的完整生命周期。当加载 beans 时,ApplicationContext 发布某些类型的事件。

    例如,当上下文启动时,ContextStartedEvent 发布,当上下文停止时,ContextStoppedEvent 发布。

    通过 ApplicationEvent 类和 ApplicationListener 接口来提供在 ApplicationContext 中处理事件。

    如果一个 bean 实现 ApplicationListener,那么每次 ApplicationEvent 被发布到 ApplicationContext 上,那个 bean 会被通知。

    Spring 提供了以下的标准事件:

    由于 Spring 的事件处理是单线程的,所以如果一个事件被发布,直至并且除非所有的接收者得到的该消息,该进程被阻塞并且流程将不会继续。

    监听上下文事件

    为了监听上下文事件,一个 bean 应该实现只有一个方法 onApplicationEvent() 的 ApplicationListener 接口。

    因此,我们写一个例子来看看事件是如何传播的,以及如何可以用代码来执行基于某些事件所需的任务。

    • 新建Spring项目

    • 创建 Java 类 HelloWorld、CStartEventHandler、CStopEventHandler 和 MainApp

    这里是 HelloWorld.java 文件的内容:

    package hello;
    
    public class HelloWorld {
        private String message;
        public void setMessage(String message){
            this.message = message;
        }
        public String getMessage(){
            System.out.println("the  Message : " + message);
            return message;
        }
    }
    

    下面是 CStartEventHandler.java 文件的内容:

    package hello;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextStartedEvent;
    
    public class CStartEventHandler implements ApplicationListener<ContextStartedEvent>{
        public void onApplicationEvent(ContextStartedEvent event){
            System.out.println("ContextStartedEvent Received");
        }
    }
    

    下面是 CStopEventHandler.java 文件的内容:

    package hello;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextStoppedEvent;
    
    public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent> {
        public void onApplicationEvent(ContextStoppedEvent event){
            System.out.println("ContextStoppedEvent Received");
        }
    }
    

    下面是 MainApp.java 文件的内容:

    package hello;
    //import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    //import org.springframework.context.annotation.*;
    
    public class MainApp {
        public static void main(String[] args) {
            ConfigurableApplicationContext context =
                    new ClassPathXmlApplicationContext("Beans.xml");
    
            context.start();
    
            HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
            obj.getMessage();
    
            context.stop();
        }
    }
    

    下面是配置文件 Beans.xml 文件:

    <?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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:annotation-config/>
    
    <!-- Definition for  bean -->
        <bean id="helloWorld" class="hello.HelloWorld">
            <property name="message" value="你好!"/>
        </bean>
    
        <bean id="cStartEventHandler" class="hello.CStartEventHandler"/>
    
        <bean id="cStopEventHandler" class="hello.CStopEventHandler"/>
    
    </beans>
    

    运行结果

    ContextStartedEvent Received
    the  Message : 你好!
    ContextStoppedEvent Received
    

    每天学习一点点,每天进步一点点。

  • 相关阅读:
    Linux系统下的安装jdk和tomcat教程
    CentOS环境下安装jdk和tomcat
    Java的一个高性能快速深拷贝方法。Cloneable?
    AOP面向切面
    struts中实现ajax的配置信息
    上传下载固定配置
    mysql常用命令
    阿里云部署前后台项目
    PMP相关文件梳理
    面试思路总结
  • 原文地址:https://www.cnblogs.com/youcoding/p/12797251.html
Copyright © 2020-2023  润新知