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的依赖,可以比较容易从现有的框架中脱离。
分享到:
相关推荐
com-spring-ioc-demo:源码主要是学习Spring IOC的原理,以及对Bean的注册及控制,主要运用以下类对Spring进行扩展学习:BeanPostProcessor,BeanFactoryAware,BeanNameAware,ApplicationContextAware,FactoryBean...
springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源
通过aware接口,可以对spring相应...首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递进来。 这样,bean 就获得了ApplicationContext这个资源
(5)如果Bean实现了ApplicationContextAware接口,则Spring调用setApplicationContext()方法传入当前ApplicationContext实例的引用。 (6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法...
Spring boot 基础 1 1.什么是spring boot 1 2.spring boot的优点 2 3.环境搭建 3 3.1 添加依赖 3 ...11.8 ApplicationContextAware接口 28 11.9 过滤器\监听器\拦截器 29 12. 注解 31 13 . 延伸 32
public class LoginAction implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws ...
4.6.2 ApplicationContextAware 和BeanNameAware . 75 4.6.3 其它Aware 接口 75 4.7 Bean 定义的继承. 77 4.8 容器扩展点. 78 4.8.1 使用BeanPostProcessor 来自定义bean 78 4.8.1.1 示例:...
ApplicationContextAware MessageSourceAware ApplicationEventPublisherAware ResourceLoaderAware 多线程 计划任务 cron fixDelay fixRate 条件注解@Conditional 组合注解与元注解 ...
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
ApplicationContextAware ApplicationContextAwareProcessor ApplicationContextException ApplicationEvent ApplicationEventMulticaster ApplicationEventPublisher ApplicationEventPublisherAware ...
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
1. 目录 1. 2. 目录 .........................................................................................................................................................1 JVM ........................
统一建模语言(UML)是一个通用的可视化建模语言,用于对软件进行描述、可视化处理、构造和建立软件系统制品的文档。它记录了对必须构造的系统的决定和理解,可用于对系统的理解、设计、浏览、配置、维护和信息控制...
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
1.继承ApplicationContextAware 2.代码如下 3.记得要把这个工具类加入到Spring的扫描路径下,这样才能在初始化时注入 4.使用方法
介绍了Java获取Bean容器的几种常见调用方式,其中详细写出了如何实现ApplicationContextAware 接口来获取Bean容器的工具类
为何@Autowired可以注入ApplicationContext, 一般来说,我们可以通过实现ApplicationContextAware接口来获取ApplicationContext的引用。但是根据官方文档,发现也可以通过 @Autowired来注入ApplicationContext,这...
自定义标签中@Autowired的属性为null 解决办法:两步 1.新建一个类SpringContext,实现接口ApplicationContextAware; 2.spring.xml中添加 3.使用SpingContext.getBean("bean名");获取