`

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多容器层面)

    在Spring框架中,`Spring-Aware`接口是一个重要的概念,它允许我们与Spring的应用上下文(ApplicationContext)进行交互,从而获取或操作由Spring管理的Bean。`ApplicationContextAware`是其中的一个典型接口,当...

    spring入门 aware接口实现

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

    Spring特性——Aware感知特性

    在Spring框架中,Aware接口系列是其核心特性之一,它为Spring容器提供了向bean注入上下文信息的能力。这些接口让bean能够感知到Spring容器的存在,从而获取必要的服务或配置信息。下面我们将深入探讨Spring的Aware...

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

    在该文件中,我们定义了一个 SpringAware Bean 对象,该对象实现了 ApplicationContextAware 接口。我们可以使用该 Bean 对象来访问到ApplicationContext 对象,然后使用该对象来访问其他 Bean 对象。 Spring Aware...

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

    Aware接口是Spring框架中的一种机制,通过实现Aware接口,可以获取Spring容器中的bean对象。在Spring编程中,经常需要根据bean的名称来获取相应的bean对象,这时候,可以通过实现BeanFactoryAware和...

    Spring中关于Bean的管理的课件

    10. **Aware接口**:Spring提供了一系列的Aware接口,如BeanNameAware、BeanFactoryAware和ApplicationContextAware,这些接口使得Bean可以在运行时获取自身的一些信息,例如Bean的名字、所处的Bean工厂或...

    Spring源码流程图

    下面我们将深入探讨Spring源码流程图,了解核心组件、Bean定义信息、Bean工厂、Bean生命周期和Aware接口的作用。 核心组件解析 在Spring框架中,核心组件包括BeanFactory、BeanDefinitionReader、...

    spring4 中文API

    - **The Resource Loader Aware interface**:介绍了Resource Loader Aware接口及其使用。 - **Resources as dependencies**:讲解了如何将资源作为依赖项使用。 - **Application contexts and Resource paths**:...

    Spring官方文档之核心篇

    - **Aware接口**:通过实现特定的Aware接口,Bean可以获得Spring容器中的资源和信息。 - **自定义属性**:开发者可以定义自己的Aware接口来实现更复杂的功能。 ### 总结 Spring官方文档的核心篇为开发者提供了关于...

    Spring源码分析.docx

    在 Spring 框架中,Bean 对象的创建过程是一个复杂的过程,涉及到多个步骤和接口。下面将对 Spring 源码中 Bean 对象的创建过程进行分析和解释。 1. 创建工厂,到达 BeanDefinition 步骤 在 Spring 框架中,Bean ...

    详解Spring中Bean的生命周期和作用域及实现方式

    6. 实现 Aware 接口:在这个阶段,Bean已经实例化,并且已经实现了Aware接口。 7. Initialize:在这个阶段,Bean已经实例化,并且已经进行了初始化。 8. 使用Bean:在这个阶段,Bean已经实例化,并且已经可以使用。 ...

    Spring5.0中文开发手册

    - 其他Aware接口允许bean获取更多的上下文信息。 - **3.7 Spring Bean的继承** - 如何在一个bean上继承另一个bean的配置。 - **3.8 容器扩展点** - **3.8.1 使用BeanPostProcessor定制bean** - ...

    Spring Bean生命周期.pdf

    - Aware接口:包括BeanNameAware、BeanClassLoaderAware、BeanFactoryAware、EnvironmentAware、EmbeddedValueResolverAware和ApplicationContextAware等。这些接口允许Bean获取容器的相关信息,如Bean的名称、类...

    详解如何在低版本的Spring中快速实现类似自动配置的功能

    在低版本的Spring中实现类似自动配置的...同时,还需要结合生命周期回调和各种Aware接口,以实现Bean的定制和功能增强。通过这种方式,即使在没有高级特性支持的老版本Spring中,也能实现灵活的配置管理和功能扩展。

    spring3.1中文参考文档

    - 介绍了Spring提供的其他Aware接口。 **4.7 Bean定义的继承** - 介绍了如何使用继承来复用bean定义。 **4.8 容器扩展点** - **4.8.1 使用BeanPostProcessor来自定义bean** - 介绍了BeanPostProcessor接口的...

    spring讲义4.txt

    1. **原理概述**:SpringActionSupport继承自Struts框架中的Action基类,同时它又实现了Spring的Aware接口,这意味着它能够感知到Spring容器的存在,从而可以访问Spring管理的bean。 2. **配置说明**: - 首先,...

    Spring Reference Core Technologies

    Spring还提供了一系列其他Aware接口,可以帮助Bean更好地融入Spring环境。 #### 六、Bean定义继承 通过继承Bean定义,可以复用现有的Bean配置,减少冗余代码。这种方式在配置复杂的应用时非常有用。 总结起来,...

    Spring-quartz.zip

    本项目结合两者,展示了如何在Spring中使用静态任务和动态任务,以及如何利用Spring的Aware接口和FactoryBean的概念。 首先,让我们深入理解Spring如何管理静态任务。在Spring中,静态任务通常指的是那些在应用启动...

    Spring 简介 轻量级的J2EE 应用程序框架

    10. **其他组件**:Spring还提供了诸如BeanFactoryPostProcessor、Aware接口等其他组件,帮助开发者更好地集成Spring框架。 #### 五、Spring框架的应用场景 Spring框架广泛应用于企业级Java应用开发中,特别是在...

    Spring Reference - Core Technologies.pdf

    Spring提供了多个Aware接口,如ResourceLoaderAware、BeanClassLoaderAware等,用于让Bean感知到Spring容器的各种资源。 1.7 Bean定义继承 Bean定义可以继承自父定义,使得子定义可以继承父定义的配置属性。 1.8 ...

Global site tag (gtag.js) - Google Analytics