- 浏览: 303039 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
zhou363667565:
看到你的这个配置 有个地方有点问题:
< aop:po ...
spring ibatis 事务配置 -
wo17796452:
[b][/b][i][/i][u][/u]引用[*][img] ...
crowd Jira confluence 集成 -
wo17796452:
<input type="button&quo ...
crowd Jira confluence 集成 -
benbear2008:
这些类图呢?
Spring MVC框架类图与顺序图 -
TTLtry:
谢了 最近学习spring时候 却总是登不上官方网站 很多 ...
Spring 2.5.5 api 帮助文档 chm格式 下载
Spring中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实作这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实作BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。 ApplicationContextAware.java public interface ApplicationContextAware { void setApplicationContext(ApplicationContext context); } 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; } } PropertyGettedEvent.java package onlyfun.caterpillar; import org.springframework.context.*; public class PropertyGettedEvent extends ApplicationEvent { public PropertyGettedEvent(Object source) { super(source); } } 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()); } } <?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! ResourceLoaderAware.java public interface ResourceLoaderAware { void setResourceLoader(ResourceLoader loader); } 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; } } ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); HelloBean hello = (HelloBean) context.getBean("helloBean"); hello.setApplicationContext(context); System.out.println(hello.getHelloWord());
Bean取得BeanFactory、ApplicationContextAware的实例目的是什么,一般的目的就是要取得一些档案资源的存取、相 关讯息资源或是那些被注入的实例所提供的机制,例如ApplicationContextAware提供了publishEvent()方法,可以支持基于Observer模式的事件传播机制。
ApplicationContextAware接口的定义如下:
我们这边示范如何透过实作ApplicationContextAware注入ApplicationContext来实现事件传播,首先我们的HelloBean如下:
ApplicationContext会由Spring容器注入,publishEvent()方法需要一个继承ApplicationEvent的对象,我们的PropertyGettedEvent继承了ApplicationEvent,如下:
当ApplicationContext执行publishEvent()后,会自动寻找实作ApplicationListener接口的对象并通知其发生对应事件,我们实作了PropertyGettedListener如下:
Listener必须被实例化,这我们可以在Bean定义档中加以定义:
我们写一个测试程序来测测事件传播的运行:
执行结果会如下所示:
以上是以实作事件传播来看看实作Aware接口取得对应对象后,可以进行的动作,同样的,您也可以实作ResourceLoaderAware接口:
实作ResourceLoader的Bean就可以取得ResourceLoader的实例,如此就可以使用它的getResource()方法,这对于必须存取档案资源的Bean相当有用。
基本上,Spring虽然提供了这些Aware相关接口,然而Bean上若实现了这些界面,就算是与Spring发生了依赖,从另一个角度来看,虽然您可以直接在Bean上实现这些接口,但您也可以透过setter来完成依赖注入,例如:
注意这次我们并没有实作ApplicationContextAware,我们在程序中可以自行注入ApplicationContext实例:
就Bean而言,降低了对Spring的依赖,可以比较容易从现有的框架中脱离。
发表评论
-
入门 21 - Controller的单元测试
2011-03-14 08:09 1086现在假设您要设 ... -
Spring MVC框架类图与顺序图
2011-03-14 08:08 4895藉由了解Spring的框架组成,我们可以了解框架之中的类与 ... -
Spring入门19 - ModelAndView类别
2011-03-14 08:07 1312入门 19 - ModelAndView类别 ... -
DispatcherServlet定义档
2011-03-14 08:07 844DispatcherServlet预设使用servlet名 ... -
入门 17 - 第一个Spring MVC Web程序
2011-03-14 08:06 1077进行这个程序之前,请您先完成入门 04 - 第一个Spri ... -
入门 16 - BeanFactoryPostProcessor接口
2011-03-14 08:05 979撰写Bean定义档通常使用XML来撰写,XML阶层式的组织 ... -
入门 14 - 资源文件的取得
2011-03-14 08:04 854首先来看看ApplicationContext接口的实作 ... -
入门 13 - MessageResource接口
2011-03-14 08:03 1083ApplicationContext继承了org.spri ... -
入门 12 - ApplicationContext接口
2011-03-14 08:03 877在Spring中,BeanFactory提供的是个比较API层 ... -
入门 11 - DataSource注入
2011-03-14 08:02 961对于不同的数据库存取需求,我们使用JDBC来解决这个 ... -
入门 10 - 集合对象注入
2011-03-14 08:01 777对于像数组、java.util.List、java. ... -
入门 09 - 属性参考与自动绑定
2011-03-14 08:00 825在定义Bean时,除了直接指定值给属性值之外,还可以直接参 ... -
入门 08 - 不使用XML定义档
2011-03-14 07:59 806XML档案的阶层格式非常适用于于组态设定,也因此几 ... -
入门 07 - Bean生命周期
2011-03-14 07:59 753在Spring中,从BeanFactory取得的实 ... -
入门 06 - Bean定义档进阶读取
2011-03-14 07:58 842BeanFactory可以接受InputStrea ... -
Constructor注入
2011-03-14 07:57 1017Spring鼓励的是setter injection,但也 ... -
第一个Spring程序
2011-03-14 07:57 844首先我们要先取得Sprin ... -
入门 03 - 依赖注入DI
2011-03-14 07:56 821IoC模式基本上是一个高层的概念,在Martin Fow ... -
控制反转IoC
2011-03-14 07:55 801IoC全名Inversion of Control,如果 ... -
简介 Spring
2011-03-14 07:54 753入门 01 - 简介 Spring Spring是轻量 ...
相关推荐
Self-Aware Computing Systems ISBN-10 书号: 3319474723 ISBN-13 书号: 9783319474724 Edition 版本: 1st ed. 2017 出版日期: 2017-01-23 pages 页数: (722 ) $219.99 This book provides formal and informal ...
CVPR 2022 Image Dehazing Transformer with Transmission-Aware 3D Position Embedding 源代码,很有学习价值,大佬任文琦团队的最新去雾杰作,从3D定位的角度去思考去雾。CVPR 2022 Image Dehazing Transformer ...
Interest-aware Message-Passing GCN for Recommendat.md
2007年Shai Avidan 和Ariel Shamir论文《Seam Carving for Content-Aware Image Resizing》所提出方法的Matlab代码实现和作者的论文。 效果非常炫,而且代码不长
2019-ICML-YOU-Position-aware Graph Neural Networks-利用邻近锚点集,强化位置描述-rrrrr1
Context-aware composition of mobile service
Context-Aware Saliency Detection的Matlab代码,可直接运行,图片自己放,
Relation-Aware Pedestrian Attribute Recognition with Graph Convolutional Networks
ECCV2020 最新论文 Structure-Aware Human-Action Generation
Group-Aware Long- and Short-Term Graph Representation Learning for Sequential Group Recommendation
强大广告专杀 Ad-Aware SE Bulid 1.06r1免安装绿色特别版
毕业时的外文翻译原文 A Race-Aware Java Runtime
DAP Detection-Aware Pre-Training With Weak Supervision
Phase-Aware Signal Processing in Speech Communication Theory and Practice 英文pdf
一篇2009年pami文章,《Context-aware Visual Tracking》
2017-Leveraging Long and Short-term Information in Content-aware Movie Recommendation
DKN_ Deep knowledge-aware network for news recommendation.pdf
Context-Aware Saliency Detection这篇显著性论文的代码。经过测试,能够运行。
arXiv预印版论文:Towards QoS-Aware and Resource-Efficient GPU Microservices Based on Spatial Multitasking GPUs In Datacenters。
Processing for Android Create Mobile, Sensor-Aware, and VR Applications Using Processing 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源...