Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
来源:互联网 发布:js弹出div模态窗口 编辑:程序博客网 时间:2024/05/29 05:04
什么是ApplicationContext?
它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。
ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。
ApplicationEven
是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
ApplicationListener
是一个接口,里面只有一个onApplicationEvent方法。
所以自己的类在实现该接口的时候,要实装该方法。
如果在上下文中部署一个实现了ApplicationListener接口的bean,
那么每当在一个ApplicationEvent发布到 ApplicationContext时,
这个bean得到通知。其实这就是标准的Oberver设计模式。
下面给出例子:
首先创建一个ApplicationEvent实现类:
hello spring event!
the source is:hello
the address is:boylmx@163.com
the email's context is:this is a email text!
它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。
ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。
ApplicationEven
是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
ApplicationListener
是一个接口,里面只有一个onApplicationEvent方法。
所以自己的类在实现该接口的时候,要实装该方法。
如果在上下文中部署一个实现了ApplicationListener接口的bean,
那么每当在一个ApplicationEvent发布到 ApplicationContext时,
这个bean得到通知。其实这就是标准的Oberver设计模式。
下面给出例子:
首先创建一个ApplicationEvent实现类:
import org.springframework.context.ApplicationEvent;public class EmailEvent extends ApplicationEvent {/** * <p>Description:</p> */private static final long serialVersionUID = 1L;public String address; public String text;public EmailEvent(Object source) {super(source);}public EmailEvent(Object source, String address, String text) {super(source);this.address = address;this.text = text;}public void print(){System.out.println("hello spring event!");}}
给出监听器:
import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationListener;public class EmailListener implements ApplicationListener {public void onApplicationEvent(ApplicationEvent event) {if(event instanceof EmailEvent){EmailEvent emailEvent = (EmailEvent)event;emailEvent.print();System.out.println("the source is:"+emailEvent.getSource());System.out.println("the address is:"+emailEvent.address);System.out.println("the email's context is:"+emailEvent.text);}}}
applicationContext.xml文件配置:<bean id="emailListener" class="com.spring.event.EmailListener"></bean>测试类:
<pre name="code" class="java">import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); //HelloBean hello = (HelloBean) context.getBean("helloBean"); //hello.setApplicationContext(context); EmailEvent event = new EmailEvent("hello","boylmx@163.com","this is a email text!"); context.publishEvent(event); //System.out.println(); }}测试结果:
hello spring event!
the source is:hello
the address is:boylmx@163.com
the email's context is:this is a email text!
0 0
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring的监听事件ApplicationListener和ApplicationEvent及@EventListener用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听ApplicationListener 和 ApplicationEvent
- Spring 通过ApplicationListener&ApplicationEvent实现事件监听
- Spring 的 ApplicationEvent和ApplicationListener
- Spring的ApplicationEvent和ApplicationListener
- Spring ApplicationEvent和ApplicationListener
- Spring中使用ApplicationContext进行事件的监听之ApplicationEvent,ApplicationListener
- Spring 的 ApplicationEvent and ApplicationListener
- Spring 的 ApplicationEvent ,ApplicationListener,ApplicationContextAware
- Spring 的 ApplicationEvent and ApplicationListener
- Spring 的 ApplicationEvent and ApplicationListener
- Spring 的 ApplicationEvent and ApplicationListener
- Linux Shell根据进程名杀死进程
- JS转换成带两位小数的数值
- HTML5中使用不同type的属性将会呈现不同的键盘样式
- ELaticSearch——PlainElastic.net.dll 中的JsonNetSerializer
- URL编码解码原理及演示(Java演示)
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- 网络层—网际协议IP
- hive表导出
- cxf整合springmvc
- Acivity的启动模式
- 【C#】Timer、窗体大小和屏幕大小
- HBase Metrics
- 网络设备驱动程序数据结构
- Android 4.0新控件 switch的属性