ApplicationContextAware
1、实现了ApplicationContextAware接口,在Bean的实例化时会自动调用setApplicationContext()方法!
2、通过调用静态方法getBean即可获取
spring中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实作这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实作BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
Bean取得BeanFactory、ApplicationContextAware的实例目的是什么,一般的目的就是要取得一些档案资源的存取、相 关讯息资源或是那些被注入的实例所提供的机制,例如ApplicationContextAware提供了publishEvent()方法,可以支持基于Observer模式的事件传播机制。
ApplicationContextAware接口的定义如下:
ApplicationContextAware.java
public interface ApplicationContextAware {
void setApplicationContext(ApplicationContext context);
}
我们这边示范如何透过实作ApplicationContextAware注入ApplicationContext来实现事件传播,首先我们的HelloBean如下:
HelloBean.java
package onlyfun.caterpillar;
import org.springframework.context.*;
public class HelloBean implements ApplicationContextAware {
private ApplicationContext applicationContext;
private String helloWord = "Hello!World!";
public void setApplicationContext(ApplicationContext context) {
this.applicationContext = context;
}
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
}
public String getHelloWord() {
applicationContext.publishEvent(
new PropertyGettedEvent("[" + helloWord + "] is getted"));
return helloWord;
}
}
ApplicationContext会由Spring容器注入,publishEvent()方法需要一个继承ApplicationEvent的对象,我们的PropertyGettedEvent继承了ApplicationEvent,如下:
PropertyGettedEvent.java
package onlyfun.caterpillar;
import org.springframework.context.*;
public class PropertyGettedEvent extends ApplicationEvent {
public PropertyGettedEvent(Object source) {
super(source);
}
}
当ApplicationContext执行publishEvent()后,会自动寻找实作ApplicationListener接口的对象并通知其发生对应事件,我们实作了PropertyGettedListener如下:
PrppertyGettedListener.java
package onlyfun.caterpillar;
import org.springframework.context.*;
public class PropertyGettedListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
System.out.println(event.getSource().toString());
}
}
Listener必须被实例化,这我们可以在Bean定义档中加以定义:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyGetterListener" class="onlyfun.caterpillar.PropertyGettedListener"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
<property name="helloWord"><value>Hello!Justin!</value></property>
</bean>
</beans>
我们写一个测试程序来测测事件传播的运行:
Test.java
package onlyfun.caterpillar;
import org.springframework.context.*;
import org.springframework.context.support.*;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
HelloBean hello = (HelloBean) context.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
执行结果会如下所示:
log4j:WARN No appenders could be found for logger
(org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
org.springframework.context.support.ClassPathXmlApplicationContext:
displayName=[org.springframework.context.support.ClassPathXmlApplicationContext;
hashCode=33219526]; startup date=[Fri Oct 29 10:56:35 CST 2004];
root of ApplicationContext hierarchy
[Hello!Justin!] is getted
Hello!Justin!
以上是以实作事件传播来看看实作Aware接口取得对应对象后,可以进行的动作,同样的,您也可以实作ResourceLoaderAware接口:
ResourceLoaderAware.java
public interface ResourceLoaderAware {
void setResourceLoader(ResourceLoader loader);
}
实作ResourceLoader的Bean就可以取得ResourceLoader的实例,如此就可以使用它的getResource()方法,这对于必须存取档案资源的Bean相当有用。
基本上,Spring虽然提供了这些Aware相关接口,然而Bean上若实现了这些界面,就算是与Spring发生了依赖,从另一个角度来看,虽然您可以直接在Bean上实现这些接口,但您也可以透过setter来完成依赖注入,例如:
HelloBean.java
package onlyfun.caterpillar;
import org.springframework.context.*;
public class HelloBean {
private ApplicationContext applicationContext;
private String helloWord = "Hello!World!";
public void setApplicationContext(ApplicationContext context) {
this.applicationContext = context;
}
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
}
public String getHelloWord() {
applicationContext.publishEvent(new PropertyGettedEvent("[" + helloWord + "] is getted"));
return helloWord;
}
}
注意这次我们并没有实作ApplicationContextAware,我们在程序中可以自行注入ApplicationContext实例:
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
HelloBean hello = (HelloBean) context.getBean("helloBean");
hello.setApplicationContext(context);
System.out.println(hello.getHelloWord());
就Bean而言,降低了对Spring的依赖,可以比较容易从现有的框架中脱离
ApplicationContextAware的更多相关文章
- 【11-10】spring学习笔记-ApplicationContextAware
package util; /** * @author aloha_world_ * @date 2016年11月10日 下午7:50:08 * @version v1.00 * @descripti ...
- org.springframework.context.ApplicationContextAware使用理解
一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...
- ApplicationContextAware 接口
一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...
- 实现ApplicationContextAware接口时,获取ApplicationContext为null
将懒加载关闭,@Lazy(false),默认为true import org.springframework.beans.BeansException; import org.springframew ...
- Example of ApplicationContextAware in Spring--转
原文地址:http://www.concretepage.com/spring/example_applicationcontextaware_spring In spring we can get ...
- 从Spring容器中获取Bean。ApplicationContextAware
引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean.(新手勿喷) 1.我们的目的是什么? 2.方法是什么(可变的细节)? 3.方法的原理是什么(不变的本质)? 1.我们的目的是什么? ...
- spring中的DisposableBean和InitializingBean,ApplicationContextAware的用法
在spring容器初始化bean和销毁bean的以前的操作有很多种, 目前我知道的有:在xml中定义的时候用init-method和destory-method,还有一种就是定义bean的时候实现Di ...
- spring获取ApplicationContext对象的方法——ApplicationContextAware
一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...
- Spring ApplicationContextAware获取上下文
一.ApplicationContextAware 用处 Spring 提供了ApplicationContextAware类,通过它可以获取所有bean上下文. 二.怎么用? ①.定义一个工具类,去 ...
- implements ApplicationContextAware 获取spring 容器
1.新建 ApplicationContextUtil 类 ,通过实现 ApplicationContextAware 的 setApplicationContext 方法,得到context上下文: ...
随机推荐
- C/C++资料网站
1.C语言基础知识讲解 http://c-faq-chn.sourceforge.net/ccfaq/node1.html 2.C++参考手册中文版 http://zh.cppreference.co ...
- jQuery给表单设置值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- TC358743XBG:HDMI转MIPI CSI参考设计
TC358743XBG参考设计电路图如下, 功能HDMI转MIPI CSI ,通信方式:IIC,分辨率1920*1080,封装形式BGA64.
- 在htnl中,<input tyle = "text">除了text外还有几种种新增的表单元素
input标签新增属性 <input list='list_t' type="text" name='user' placeholder='请输入姓名' va ...
- 王爽汇编习题2.2(1):给定地址段为0001H,仅通过变化偏移地址寻址,CPU的寻址范围为____到____
此题解题背景默认为8080型CPU,地址总线为16根.(8080-16,8086-20,8088-20,80286-24,80386-32) 16根地址总线寻址能力:(2 ** 16) / 1024 ...
- Java 得到磁盘以及内存信息
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt217 1.得到磁盘信息 File[] files = File. listR ...
- 2017 ACM-ICPC(乌鲁木齐赛区)网络赛 H.Skiing 拓扑排序+最长路
H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...
- HTML特殊符号、常用字符实体
HTML特殊符号对照表.常用的字符实体 最常用的字符实体 显示结果 描述 实体名称 实体编号 空格 <</td> 小于号 < < > 大于号 > ...
- 团队作业10——复审和事后分析(Beta版本)
团队作业10--事后分析(Beta版本) http://www.cnblogs.com/newteam6/p/6953992.html 团队作业10--复审(Beta版本) http://www.cn ...
- Swing-JPopupMenu弹出菜单用法-入门
弹出菜单是GUI程序中非常常见的一种控件.它通常由鼠标右击事件触发,比如在windows系统桌面上右击时,会弹出一个包含“刷新”.“属性”等菜单的弹出菜单.Swing中的弹出菜单是JPopupMenu ...