`

Spring 中的Aware接口

阅读更多

Spring中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实作这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实作BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
Bean取得BeanFactory、ApplicationContextAware的实例目的是什么,一般的目的就是要取得一些档案资源的存取、相 关讯息资源或是那些被注入的实例所提供的机制,例如ApplicationContextAware提供了publishEvent()方法,可以支持基于Observer模式的事件传播机制。
ApplicationContextAware接口的定义如下:

Java代码 复制代码 收藏代码
  1. public interface ApplicationContextAware {  
  2.     void setApplicationContext(ApplicationContext context);  
  3. }  
public interface ApplicationContextAware {
    void setApplicationContext(ApplicationContext context);
}


我们这边示范如何透过实作ApplicationContextAware注入ApplicationContext来实现事件传播,首先我们的HelloBean如下:

Java代码 复制代码 收藏代码
  1. import org.springframework.context.*;  
  2. public class HelloBean implements ApplicationContextAware {  
  3.   
  4.     private ApplicationContext applicationContext;  
  5.     private String helloWord = "Hello!World!";  
  6.   
  7.     public void setApplicationContext(ApplicationContext context) {  
  8.         this.applicationContext = context;  
  9.     }  
  10.   
  11.     public void setHelloWord(String helloWord) {  
  12.         this.helloWord = helloWord;  
  13.     }  
  14.   
  15.     public String getHelloWord() {  
  16.         applicationContext.publishEvent(  
  17.                new PropertyGettedEvent("[" + helloWord + "] is getted"));  
  18.         return helloWord;  
  19.     }  
  20. }  
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,如下:

Java代码 复制代码 收藏代码
  1. import org.springframework.context.*;  
  2. public class PropertyGettedEvent extends ApplicationEvent {  
  3.     public PropertyGettedEvent(Object source) {  
  4.         super(source);  
  5.     }  
  6. }  
import org.springframework.context.*;
public class PropertyGettedEvent extends ApplicationEvent {
    public PropertyGettedEvent(Object source) {
        super(source);
    }
}


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

Java代码 复制代码 收藏代码
  1. import org.springframework.context.*;  
  2. public class PropertyGettedListener implements ApplicationListener {  
  3.     public void onApplicationEvent(ApplicationEvent event) {  
  4.         System.out.println(event.getSource().toString());     
  5.     }  
  6. }  
import org.springframework.context.*;
public class PropertyGettedListener implements ApplicationListener {
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println(event.getSource().toString());   
    }
}


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

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  4.   
  5. <beans>  
  6.     <bean id="propertyGetterListener" class="onlyfun.caterpillar.PropertyGettedListener"/>  
  7.     <bean id="helloBean" class="cn.edu.ccnu.inc.HelloBean">  
  8.         <property name="helloWord"><value>Hello!Justin!</value></property>  
  9.     </bean>  
  10. </beans>  
分享到:
评论

相关推荐

    spring-aware接口实现与bean作用域(spring多容器层面)

    使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。

    spring入门 aware接口实现

    通过aware接口,可以对spring相应资源(可能包含相关核心资源)进行操作(一定要慎重) 首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递...

    Spring Aware标记接口使用案例解析

    主要介绍了Spring Aware标记接口使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Spring实现Aware接口自定义获取bean的两种方式

    主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。

    spring3.1中文参考文档

    spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................

    Spring 3 Reference中文

    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 ...

    开源框架面试专题及答案.pdf

    Spring Bean 的生命周期 &gt; Spring Bean 的生命周期简单易懂。...&gt; 针对特殊行为的其他 Aware 接口 &gt; Bean 配置文件中的 Custom init()方法和 destroy()方法 &gt; @PostConstruct 和@PreDestroy 注解方式

    struts2.0.jar

    · 便于与Spring集成: Struts 2 Action能够感知Spring(Spring-aware)。只要为某个应用添加Spring beans,就可以添加对Spring的支持。 · 易于定制的控制器: Struts 1允许请求处理程序可按照模块来定制,在Struts ...

    java8源码-somethingnew:各种演示在这里

    Aware接口 用于在创建对象时候自动调用里面的方法 stn-apt 注解处理器学习使用,基于javapoet框架实现类的创建。 stn-cqxhat 基于netty+spring开发的简单系统 基于telnet进行cs交互 基于plugin进行扩

Global site tag (gtag.js) - Google Analytics