Spring 中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实做这些 Aware接口的Bean在被初始以后,能够取得一些相对应的资源,例如实做BeanFactoryAware的Bean在初始后,Spring容器将会 注入BeanFactory的实例,而实做ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。 Bean取得BeanFactory、ApplicationContextAware的实 例目的是什么,通常的目的就是要取得一些档案资源的存取、相 关讯息资源或是那些被注入的实例所提供的机制,例如ApplicationContextAware提供了publishEvent()方法,能够支持基 于Observer模式的事件传播机制。 ApplicationContextAware接口的定义以下:java
ApplicationContextAware.java public interface ApplicationContextAware { void setApplicationContext(ApplicationContext context); }
咱们这边示范如何透过实做ApplicationContextAware注入ApplicationContext来实现事件传播,首先咱们的HelloBean以下:spring
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,以下:app
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>
咱们写一个测试程序来测测事件传播的运行:this
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()); } }
执行结果会以下所示:code
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接口:server
ResourceLoaderAware.java public interface ResourceLoaderAware { void setResourceLoader(ResourceLoader loader); }
实做ResourceLoader的Bean就能够取得ResourceLoader的实例,如此就能够使用它的getResource()方法,这对于必须存取档案资源的Bean至关有用。 基本上,Spring虽然提供了这些Aware相关接口,然而Bean上若实现了这些界面,就算是与Spring发生了依赖,从另外一个角度来看,虽然您能够直接在Bean上实现这些接口,但您也能够透过setter来完成依赖注入,例如:xml
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的依赖,能够比较容易从现有的框架中脱离。