您的位置:首页 > 移动开发

ApplicationContextAware接口介绍

2015-09-10 11:00 399 查看





Spring中提供一些Aware相关接口,像是BeanFactoryAware、ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实现这些Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实现BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入ApplicationContext的实例等等。

Bean取得BeanFactory、ApplicationContextAware的目的是什么,一般的目的就是要取得一些资源的存取、或是使用那些被注入的实例所提供的机制,例如ApplicationContextAware提供了publishEvent()方法,可以支持基于Observer模式的事件传播机制。

ApplicationContextAware接口的定义如下:

1

2

3

4

ApplicationContextAware.java

publicinterfaceApplicationContextAware{

voidsetApplicationContext(ApplicationContextcontext);

}

下面说明一下如何通过实现ApplicationContextAware注入ApplicationContext来实现事件传播,首先编写HelloBean如下:

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;

}

}

ApplicationContext会由Spring容器注入,publishEvent()方法需要一个继承ApplicationEvent的对象,我们的PropertyGettedEvent继承了ApplicationEvent,如下:

1

2

3

4

5

6

7

PropertyGettedEvent.java

importorg.springframework.context.*;

publicclassPropertyGettedEventextendsApplicationEvent{

publicPropertyGettedEvent(Objectsource){

super(source);

}

}

当ApplicationContext执行publishEvent()后,会自动寻找实作ApplicationListener接口的对象并通知其发生对应事件,我们实作了PropertyGettedListener如下:

PrppertyGettedListener.java

1

2

3

4

5

6

importorg.springframework.context.*;

publicclassPropertyGettedListenerimplementsApplicationListener{

publicvoidonApplicationEvent(ApplicationEventevent){

System.out.println(event.getSource().toString());

}

}

Listener必须被实例化,这我们可以在Bean定义档中加以定义:

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);

}

实现ResourceLoader的Bean就可以取得ResourceLoader的实例,如此就可以使用它的getResource()方法,这对于必须存取资源的Bean相当有用。

基本上,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;

}

}

注意这次我们并没有实现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接口介绍

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: