目录
applicationcontext 是spring的核心模块,管理着beans完整的生命周期。当加载bean时,applicationcontext会发布特定类型的事件。
eg:当context启动时contextstartevent被发布,当关闭时,contextstoppedevent被发布。
applicationcontext事件处理被applicationevent类和applicationlistener接口提供。因此,实现了applicationlistener的bean,每次applicationcontext发布applicationevent时,bean将会被通知。
spring的事件处理是单线程的,因此如果发布了一个事件,直到并且除非所有接收者都得到消息,否则进程将被阻塞,线程将不会继续。因此,如果要使用事件处理,那么在设计应用程序时应该小心。
要想监听一个context事件,bean需要实现仅有一个方法onapplicationevent()的applicationlistener接口
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
下边的案例将讲述如何编写和发布你自己的自定义事件
添加自定义事件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
如对本文有疑问, 点击进行留言回复!!
SpringBoot引用阿里easyexcel,Excel导出返回浏览器下载
HashMap、Hashtable、ConcurrentHashMap三者间的异同
解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题
多线程、同步工作原理、死锁案例、Lock接口、线程的生命周期的讲解及实现
网友评论