当前位置: 移动技术网 > IT编程>开发语言>Java > Spring入门学习笔记(3)——事件处理类

Spring入门学习笔记(3)——事件处理类

2018年10月15日  | 移动技术网IT编程  | 我要评论

目录

spring中的事件处理

applicationcontext 是spring的核心模块,管理着beans完整的生命周期。当加载bean时,applicationcontext会发布特定类型的事件。
eg:当context启动时contextstartevent被发布,当关闭时,contextstoppedevent被发布。

applicationcontext事件处理被applicationevent类和applicationlistener接口提供。因此,实现了applicationlistener的bean,每次applicationcontext发布applicationevent时,bean将会被通知。

spring内建事件

  • contextrefreshedevent : 当applicationcontext被初始化或者刷新时被发布。也可以通过调用configurableapplicationcontext接口的refresh()函数发起。
  • contextstartedevent : 当application使用configurableapplicationcontext的start()方法启动时被发布。您可以轮询您的数据库,也可以在收到此事件后重新启动任何已停止的应用程序。
  • contextstoppedevent : 当applicationcontext在configurableapplicationcontext接口上使用stop()方法停止时,就会发布这个事件。你可以在收到这个活动后做家务。
  • contextclosedevent : 当使用configurableapplicationcontext接口上的close()方法关闭applicationcontext时,将发布此事件。一个封闭的环境到达了生命的终点;不能刷新或重新启动。
  • requesthandledevent : 这是一个特定于web的事件,它告诉所有bean http请求已经得到了服务。

spring的事件处理是单线程的,因此如果发布了一个事件,直到并且除非所有接收者都得到消息,否则进程将被阻塞,线程将不会继续。因此,如果要使用事件处理,那么在设计应用程序时应该小心。

监听context事件

要想监听一个context事件,bean需要实现仅有一个方法onapplicationevent()的applicationlistener接口

example

helloworld.java

public class helloworld {
   private string message;

   public void setmessage(string message){
      this.message  = message;
   }
   public void getmessage(){
      system.out.println("your message : " + message);
   }
}

cstarteventhandler.java

public class cstarteventhandler 
   implements applicationlistener<contextstartedevent>{

   public void onapplicationevent(contextstartedevent event) {
      system.out.println("contextstartedevent received");
   }
}

cstopeventhandler

public class cstopeventhandler 
   implements applicationlistener<contextstoppedevent>{

   public void onapplicationevent(contextstoppedevent event) {
      system.out.println("contextstoppedevent received");
   }
}

mainapp.java

public class mainapp {
   public static void main(string[] args) {
      configurableapplicationcontext context = 
         new classpathxmlapplicationcontext("beans.xml");

      // let us raise a start event.
      context.start();
      
      helloworld obj = (helloworld) context.getbean("helloworld");
      obj.getmessage();

      // let us raise a stop event.
      context.stop();
   }
}

beans

<?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.tutorialspoint.helloworld">
      <property name = "message" value = "hello world!"/>
   </bean>

   <bean id = "cstarteventhandler" class = "com.tutorialspoint.cstarteventhandler"/>
   <bean id = "cstopeventhandler" class = "com.tutorialspoint.cstopeventhandler"/>

</beans>

输出:

contextstartedevent received
your message : hello world!
contextstoppedevent received

自定义spring事件

下边的案例将讲述如何编写和发布你自己的自定义事件

添加自定义事件customevent.java,继承applicationevent类

public class customevent extends applicationevent{
   public customevent(object source) {
      super(source);
   }
   public string tostring(){
      return "my custom event";
   }
}

添加自定义事件发布类customeventpublisher.java,实现applicationeventpublisheraware接口

public class customeventpublisher implements applicationeventpublisheraware {
   private applicationeventpublisher publisher;
   
   public void setapplicationeventpublisher (applicationeventpublisher publisher) {
      this.publisher = publisher;
   }
   public void publish() {
      customevent ce = new customevent(this);
      publisher.publishevent(ce);
   }
}

自定义事件监听处理类,实现applicationlistener

public class customeventhandler implements applicationlistener<customevent> {
   public void onapplicationevent(customevent event) {
      system.out.println(event.tostring());
   }
}

mainapp.java

public class mainapp {
   public static void main(string[] args) {
      configurableapplicationcontext context = 
         new classpathxmlapplicationcontext("beans.xml");
      
      customeventpublisher cvp = 
         (customeventpublisher) context.getbean("customeventpublisher");
      
      cvp.publish();  
      cvp.publish();
   }
}

beans.xml

<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 = "customeventhandler" class = "com.tutorialspoint.customeventhandler"/>
   <bean id = "customeventpublisher" class = "com.tutorialspoint.customeventpublisher"/>

</beans>

注意不要忘记添加customeventhandler,虽然在主函数中没有直接使用,但是context需要检查实现了applicationlistener的
bean,所以需要在xml文件中,添加该bean。

输出:

my custom event
my custom event

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网