Spring中的Aware和事件
来源:互联网 发布:爱思助手 for mac 编辑:程序博客网 时间:2024/06/07 17:17
一、Aware相关接口
1,Spring中提供一些Aware相关接口,像是BeanNameAware、ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实现这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实作BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
2,对于 ServletContextAware,只有在Web容器中运行时才有值。
二、事件(Spring Reference Documentation 3.8.3. Events)
1,Spring的ApplicationContext会自动在当前的所有Bean中寻找ApplicationListener接口的实现,并将其作为事件接收对象。当ApplicationContext.publishEvent()方法调用时,所有定义的ApplicationListener接口的实现类都会被触发,每个ApplicationListener可根据事件的类型判断是否是自己需要处理的事件。观察者模式(Observer,此种模式通常被用来实作事件处理系统)。
2,主要用到的spring的类和接口有:
1,事件类:org.springframework.context.ApplicationEvent(抽象类),其子类来实现事件:
1,ContextRefreshedEvent在ApplicationContext初始化或Refresh(调用refresh()方法)时发布事件
2,ContextClosedEvent在ApplicationContext关闭(调用close()方法)时发布事件
3,ContextStartedEvent在调用start()方法时发布事件
4,ContextStoppedEvent在调用end()方法时发布事件
5,RequestHandledEvent无
2,事件监听器:org.springframework.context.ApplicationListener (接口),其实现类实现事件的监听。
在ApplicationListener的实现类中可以只处理自己关心的事件,如:
public class MyListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
System.out.println("ApplicationContext初始化或刷新了.");
}
}
}
3,发布事件:
ApplicationContext.publishEvent(ApplicationEvent event); // 会触发所有已定义的ApplicationListener实现类
3,测试:
public class SpringListenerTest {
@Test
public void test() {
// 要使用具体实现类,才有refresh()等方法
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ac.publishEvent(new MyEvent(ac));
ac.refresh();
ac.close();
}
}
class MyEvent extends ApplicationEvent {
public MyEvent(Object source) {
super(source);
}
}
@Component
class MyListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("----> event = " + event);
}
}
1,Spring中提供一些Aware相关接口,像是BeanNameAware、ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实现这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实作BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
2,对于 ServletContextAware,只有在Web容器中运行时才有值。
二、事件(Spring Reference Documentation 3.8.3. Events)
1,Spring的ApplicationContext会自动在当前的所有Bean中寻找ApplicationListener接口的实现,并将其作为事件接收对象。当ApplicationContext.publishEvent()方法调用时,所有定义的ApplicationListener接口的实现类都会被触发,每个ApplicationListener可根据事件的类型判断是否是自己需要处理的事件。观察者模式(Observer,此种模式通常被用来实作事件处理系统)。
2,主要用到的spring的类和接口有:
1,事件类:org.springframework.context.ApplicationEvent(抽象类),其子类来实现事件:
1,ContextRefreshedEvent在ApplicationContext初始化或Refresh(调用refresh()方法)时发布事件
2,ContextClosedEvent在ApplicationContext关闭(调用close()方法)时发布事件
3,ContextStartedEvent在调用start()方法时发布事件
4,ContextStoppedEvent在调用end()方法时发布事件
5,RequestHandledEvent无
2,事件监听器:org.springframework.context.ApplicationListener (接口),其实现类实现事件的监听。
在ApplicationListener的实现类中可以只处理自己关心的事件,如:
public class MyListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
System.out.println("ApplicationContext初始化或刷新了.");
}
}
}
3,发布事件:
ApplicationContext.publishEvent(ApplicationEvent event); // 会触发所有已定义的ApplicationListener实现类
3,测试:
public class SpringListenerTest {
@Test
public void test() {
// 要使用具体实现类,才有refresh()等方法
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ac.publishEvent(new MyEvent(ac));
ac.refresh();
ac.close();
}
}
class MyEvent extends ApplicationEvent {
public MyEvent(Object source) {
super(source);
}
}
@Component
class MyListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("----> event = " + event);
}
}
0 0
- Spring中的Aware和事件
- Spring:Aware和事件
- Spring中的Aware
- Spring中的Aware接口
- Spring框架中的各种*Aware接口
- Spring框架中的各种*Aware接口
- Spring-Aware
- # Spring Aware
- Spring Aware
- Spring Aware
- Spring Aware (Spring 感知)
- spring-aware接口
- spring-Aware接口
- Spring Bean--Aware接口
- Spring -- 3.Aware
- spring之aware接口
- spring的Aware
- Spring Aware理解
- Android 图片压缩上传问题.
- Contacts-Framework获取通讯录
- 5.10.3 顶点属性内插
- 从零开始制作H5应用(2)——V2.0版,多页单张图片滑动,透明过渡及交互指示
- C#连接SQLite的字符串
- Spring中的Aware和事件
- WebView Demo
- Asp.net中DataBinder.Eval用法的总结
- linux下的分区及挂载
- 关于Oracle事务的总结
- 物理层学习结构
- Unity3d学习03
- 【数据结构与算法】 Dijkstra算法
- tomcat 终止端口号