Event Handling in Spring

Spring内置的event有

1.ContextRefreshedEvent

This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface.

2.ContextStartedEvent
This event is published when the ApplicationContext is started using the start() method on theConfigurableApplicationContext interface. You can poll your database or you can re/start any stopped application after receiving this event.

3.ContextStoppedEvent
This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface. You can do required housekeep work after receiving this event.

4.ContextClosedEvent
This event is published when the ApplicationContext is closed using the close() method on theConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted.

5.RequestHandledEvent
This is a web-specific event telling all beans that an HTTP request has been serviced.

Spring's event handling is single-threaded so if an event is published, until and unless all the receivers get the message, the processes are blocked and the flow will not continue. Hence, care should be taken when designing your application if event handling is to be used.

下面我们来测试一下

首先我们写一个helloword

package com.hyenas.spring.event;

public class HelloWorld {

    private String msg;

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

    public void getMsg() {
        System.out.println("your message:" + msg);
    }

}

CStartEventHandler.java

package com.hyenas.spring.event;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;

public class CStartEventHandler implements ApplicationListener<ContextStartedEvent>{

    @Override
    public void onApplicationEvent(ContextStartedEvent arg0) {
        System.out.println("ContextStartedEvent Received");
    }
    

}

CRefreshedEventHandler.java

package com.hyenas.spring.event;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class CRefreshedEventHandler implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
        System.out.println("ContextRefreshedEvent received");
    }
    
}

CStopEventHandler.java

package com.hyenas.spring.event;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;

public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{

    @Override
    public void onApplicationEvent(ContextStoppedEvent arg0) {
        System.out.println("ContextStoppedEvent received");
    }

}

然后我们配置一个Spring 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.hyenas.spring.event.HelloWorld">
       <property name="msg" value="Hello World!"/>
   </bean>

   <bean id="cStartEventHandler" 
         class="com.hyenas.spring.event.CStartEventHandler"/>

   <bean id="cStopEventHandler" 
         class="com.hyenas.spring.event.CStopEventHandler"/>
   
   <bean id="cRefreshedEventHandler" 
         class="com.hyenas.spring.event.CRefreshedEventHandler"/>

</beans>

现在我们来测试一下我们写的东西:

MainApp.java

package com.hyenas.spring.event;

import org.springframework.beans.BeansException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = null;
        try {
            context = new ClassPathXmlApplicationContext("event-handler.xml");

            // Let us raise a start event.
            context.start();

            HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

            obj.getMsg();

            // Let us raise a refresh event
            context.refresh();
            
            // Let us raise a stop event.
            context.stop();
        } catch (BeansException e) {
            if (context != null) {
                context.close();
            }
        }
        
    }
}

运行结果:

ContextRefreshedEvent received
ContextStartedEvent Received
your message:Hello World!
ContextRefreshedEvent received
ContextStoppedEvent received

原文地址:https://www.cnblogs.com/hupengcool/p/3629134.html