Spring中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实作这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实作BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
Bean取得BeanFactory、ApplicationContextAware的实例目的是什么,一般的目的就是要取得一些档案资源的存取、相 关讯息资源或是那些被注入的实例所提供的机制,例如ApplicationContextAware提供了publishEvent()方法,可以支持基于Observer模式的事件传播机制。
ApplicationContextAware接口的定义如下:
- public interface ApplicationContextAware {
- void setApplicationContext(ApplicationContext context);
- }
public interface ApplicationContextAware { void setApplicationContext(ApplicationContext context); }
我们这边示范如何透过实作ApplicationContextAware注入ApplicationContext来实现事件传播,首先我们的HelloBean如下:
- 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;
- }
- }
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,如下:
- import org.springframework.context.*;
- public class PropertyGettedEvent extends ApplicationEvent {
- public PropertyGettedEvent(Object source) {
- super(source);
- }
- }
import org.springframework.context.*; public class PropertyGettedEvent extends ApplicationEvent { public PropertyGettedEvent(Object source) { super(source); } }
当ApplicationContext执行publishEvent()后,会自动寻找实作ApplicationListener接口的对象并通知其发生对应事件,我们实作了PropertyGettedListener如下:
- import org.springframework.context.*;
- public class PropertyGettedListener implements ApplicationListener {
- public void onApplicationEvent(ApplicationEvent event) {
- System.out.println(event.getSource().toString());
- }
- }
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="cn.edu.ccnu.inc.HelloBean">
- <property name="helloWord"><value>Hello!Justin!</value></property>
- </bean>
- </beans>
相关推荐
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
通过aware接口,可以对spring相应资源(可能包含相关核心资源)进行操作(一定要慎重) 首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递...
主要介绍了Spring Aware标记接口使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
4.6.3 其它Aware 接口 75 4.7 Bean 定义的继承. 77 4.8 容器扩展点. 78 4.8.1 使用BeanPostProcessor 来自定义bean 78 4.8.1.1 示例:BeanPostProcessor 风格的Hello World.. 79 4.8.1.2 ...
Spring Bean 的生命周期 > Spring Bean 的生命周期简单易懂。...> 针对特殊行为的其他 Aware 接口 > Bean 配置文件中的 Custom init()方法和 destroy()方法 > @PostConstruct 和@PreDestroy 注解方式
· 便于与Spring集成: Struts 2 Action能够感知Spring(Spring-aware)。只要为某个应用添加Spring beans,就可以添加对Spring的支持。 · 易于定制的控制器: Struts 1允许请求处理程序可按照模块来定制,在Struts ...
Aware接口 用于在创建对象时候自动调用里面的方法 stn-apt 注解处理器学习使用,基于javapoet框架实现类的创建。 stn-cqxhat 基于netty+spring开发的简单系统 基于telnet进行cs交互 基于plugin进行扩