Event Handling in Spring

时间:2014-03-28 12:50:12   收藏:0   阅读:597

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

bubuko.com,布布扣
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);
    }

}
bubuko.com,布布扣

 

CStartEventHandler.java

bubuko.com,布布扣
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");
    }
    

}
bubuko.com,布布扣

CRefreshedEventHandler.java

bubuko.com,布布扣
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");
    }
    
}
bubuko.com,布布扣

CStopEventHandler.java

bubuko.com,布布扣
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");
    }

}
bubuko.com,布布扣

然后我们配置一个Spring xml

bubuko.com,布布扣
<?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>
bubuko.com,布布扣

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

MainApp.java

bubuko.com,布布扣
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();
            }
        }
        
    }
}
bubuko.com,布布扣

运行结果:

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

Event Handling in Spring,布布扣,bubuko.com

原文:http://www.cnblogs.com/hupengcool/p/3629134.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!