ApplicationContextAware接口介绍
2015-09-10 11:00
399 查看
Bean取得BeanFactory、ApplicationContextAware的目的是什么,一般的目的就是要取得一些资源的存取、或是使用那些被注入的实例所提供的机制,例如ApplicationContextAware提供了publishEvent()方法,可以支持基于Observer模式的事件传播机制。
ApplicationContextAware接口的定义如下:
1 2 3 4 | ApplicationContextAware.java publicinterfaceApplicationContextAware{ voidsetApplicationContext(ApplicationContextcontext); } |
HelloBean.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | importorg.springframework.context.*; publicclassHelloBeanimplementsApplicationContextAware{ privateApplicationContextapplicationContext; privateStringhelloWord="Hello!World!"; publicvoidsetApplicationContext(ApplicationContextcontext){ this.applicationContext=context; } publicvoidsetHelloWord(StringhelloWord){ this.helloWord=helloWord; } publicStringgetHelloWord(){ applicationContext.publishEvent(newPropertyGettedEvent("["+helloWord+"] is getted")); returnhelloWord; } } |
1 2 3 4 5 6 7 | PropertyGettedEvent.java importorg.springframework.context.*; publicclassPropertyGettedEventextendsApplicationEvent{ publicPropertyGettedEvent(Objectsource){ super(source); } } |
PrppertyGettedListener.java
1 2 3 4 5 6 | importorg.springframework.context.*; publicclassPropertyGettedListenerimplementsApplicationListener{ publicvoidonApplicationEvent(ApplicationEventevent){ System.out.println(event.getSource().toString()); } } |
1 2 3 4 5 6 7 8 | <?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEbeansPUBLIC"-//SPRING/DTD BEAN/EN""http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <beanid="propertyGetterListener"class="PropertyGettedListener"/> <beanid="helloBean"class="HelloBean"> <propertyname="helloWord"><value>HelloSwiftlet!</value></property> </bean> </beans> |
1 2 3 4 5 6 7 8 9 | importorg.springframework.context.*; importorg.springframework.context.support.*; publicclassTest{ publicstaticvoidmain(String[]args){ ApplicationContextcontext=newClassPathXmlApplicationContext("bean.xml"); HelloBeanhello=(HelloBean)context.getBean("helloBean"); System.out.println(hello.getHelloWord()); } } |
[Hello Swiftlet!] is getted
Hello Swiftlet!
以上是以实现事件传播来看看实作Aware接口取得对应对象后,可以进行的动作,同样的,你也可以实作ResourceLoaderAware接口:
ResourceLoaderAware.java
1 2 3 | publicinterfaceResourceLoaderAware{ voidsetResourceLoader(ResourceLoaderloader); } |
基本上,Spring虽然提供了这些Aware相关接口,然而Bean上若实现了这些接口,就算是与Spring发生了依赖,从另一个角度来看,虽然你以直接在Bean上实现这些接口,但也可以透过setter来完成依赖注入,例如:
HelloBean.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | importorg.springframework.context.*; publicclassHelloBean{ privateApplicationContextapplicationContext; privateStringhelloWord="Hello!World!"; publicvoidsetApplicationContext(ApplicationContextcontext){ this.applicationContext=context; } publicvoidsetHelloWord(StringhelloWord){ this.helloWord=helloWord; } publicStringgetHelloWord(){ applicationContext.publishEvent(newPropertyGettedEvent("["+helloWord+"] is getted")); returnhelloWord; } } |
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
HelloBean hello = (HelloBean) context.getBean("helloBean");
hello.setApplicationContext(context);
System.out.println(hello.getHelloWord());
就Bean而言,降低了对Spring的依赖,可以比较容易从现有的框架中脱离。
声明: 本文由金丝燕网原创编译,转载请保留链接: ApplicationContextAware接口介绍
相关文章推荐
- 解读Android之Service(2)Bound Service
- Android动画-FrameAnimation
- Android Listview多次调用相同的addHeaderView和addFooterView 问题
- Android 之ko模块的自动加载
- 【Android基础】——AsyncTask
- 关于iOS 真机不能调试process launch failed: timed out waiting for app to launch 和 failed to get the task for
- Android动画-TweenAnimation
- iOS获取当前时区
- Android拦截、监听系统返回键事件
- IOS 实现应用间授权登录、从一个应用跳到另一个应用,跳到app下载应用、评分。
- ios-裁剪加裁剪描边加把裁剪封装成一个方法类
- Android国际化
- 运用IntentService类下载文件
- 解决微信内置浏览器tel:手机号不能打电话的问题
- webappbuilder改变leftpanel的大小
- Android常用组件
- iOS技术视频 这个是浩哥给大家录得视频,主要针对是拔高的
- ios开发日记-2 iOS图片拉伸技巧
- 盘点国内外在线app制作平台:分分钟做款app
- iOS学习 -- 中文按首字母拼音排序