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的依赖,可以比较容易从现有的框架中脱离。

spring中一些aware接口的更多相关文章

  1. spring中的aware接口

    1.实现了相应的aware接口,这个类就获取了相应的资源. 2.spring中有很多aware接口,包括applicationContextAware接口,和BeanNameAware接口. 实现了这 ...

  2. spring中基础核心接口总结

    spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...

  3. Spring中的InitializingBean接口的使用

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法. 测试,如下: imp ...

  4. 谈谈Spring中的BeanPostProcessor接口

    一.前言   这几天正在复习Spring的相关内容,在了解bean的生命周期的时候,发现其中涉及到一个特殊的接口--BeanPostProcessor接口.由于网上没有找到比较好的博客,所有最后花了好 ...

  5. Spring 中的 MetaData 接口

    什么是元数据(MetaData) 先直接贴一个英文解释: Metadata is simply data about data. It means it is a description and co ...

  6. Spring中的InitializingBean接口

    InitializingBean接口为bean提供了初始化方法的方式,它只有afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: impo ...

  7. spring源码:Aware接口(li)

    一.spring容器中的aware接口介绍 Spring中提供了各种Aware接口,比较常见的如BeanFactoryAware,BeanNameAware,ApplicationContextAwa ...

  8. spring源码:Aware接口

    一.spring容器中的aware接口介绍 Spring中提供了各种Aware接口,比较常见的如BeanFactoryAware,BeanNameAware,ApplicationContextAwa ...

  9. MyBatis-Spring中间件逻辑分析(怎么把Mapper接口注册到Spring中)

    1.      文档介绍 1.1.      为什么要写这个文档 接触Spring和MyBatis也挺久的了,但是一直还停留在使用的层面上,导致很多时候光知道怎么用,而不知道其具体原理,这样就很难做一 ...

随机推荐

  1. 01_Linux系统系统语言查询,设置Xshell工具,中文显示,测试Xshell中文字符显示,Linux中文显示乱码设置

              Xshell是一个强大的安全终端模拟软件,它支持SSH1,SSH2,以及Microsoft Windows平台的TELNETNetSarang Xshell 4 Build 0 ...

  2. C++对C的函数拓展 - 占位参数

    函数占位参数 占位参数只有参数类型声明,而没有参数名声明 一般情况下,在函数体内部无法使用占位参数 demo #include <iostream> using namespace std ...

  3. 版本控制之最佳实践(Git版)

    现如今,应该每个开发者都在使用版本控制工具了吧.然而,如果你理解版本控制的基本规则,你便能更好地发挥它的效用.在此,我们汇总了一些最佳实践,希望你在使用Git做版本控制时能够了然于心.得心应手. 1. ...

  4. mixer: mysql协议分析

    综述 要实现一个mysql proxy,首先需要做的就是理解并实现mysql通讯协议.这样才能通过proxy架起client到server之间的桥梁. mixer的mysql协议实现主要参考mysql ...

  5. Maven nexus安装、配置和使用

    简介         Nexus 可以代理并缓存 Maven 构件,当 Maven 需要下载构件的时候,就不需要反复的请求中央仓库. 有些公司都不提供外网给项目组人员,因此就不能使用 Maven 访问 ...

  6. ubuntu wubi.exe 直接加载下载好的 amd64.tar.xz

    玩了这么久的LINUX,一直都是直机装UBUNTU,虚一下XP的,后来不得不直机用WIN7,只能WUBI装一下UBUNTU了.不得不说,在WIN7下虚一个UBUNTU真是相当麻烦.网络那块很是难搞,而 ...

  7. 关于jQuery中的trigger和triggerHandler方法的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Ext.Net 1.X_读写配置文件

    [摘要] 有N个ERP数据库帐套,需要从XML文件中读取. 加载指定路径的XML /// <summary> /// 取得帐套列表 /// </summary> private ...

  9. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

  10. SwipeRefreshLayout实现上拉下拉刷新

    1:在布局中添加SwipeRefreshLayout和Listview组件 [html] view plain copy <?xml version="1.0" encodi ...