美文网首页
Aware Mode

Aware Mode

作者: 若兮缘 | 来源:发表于2019-01-19 22:09 被阅读45次

Aware接口

Spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以获取相应资源
通过Aware接口,可以对Spring相应资源进行操作(使用需慎重)
为对Spring进行简单的扩展提供了方便的入口

ApplicationContextAware

实现了ApplicationContextAware接口的类将会获得ApplicationContext实例的引用

BeanNameAware

实现BeanNameAware接口的类将会获得该类对象的bean定义的name值(id)

主要Aware子接口

Aware相关示例

ApplicationContextAware

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class MoocApplicationContext implements ApplicationContextAware  {
    //定义成员变量
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        //获取spring上下文进行相关操作
        this.applicationContext = applicationContext;
        System.out.println("MoocApplicationContext : " + applicationContext.getBean("moocApplicationContext").hashCode());
    }
    
}

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
    
    public TestAware() {
        super("classpath:spring-aware.xml");
    }
    
    @Test
    public void testMoocApplicationContext() {
        System.out.println("testMoocApplicationContext : " + super.getBean("moocApplicationContext").hashCode());
        //执行结果为: MoocApplicationContext : 1795799895
        //           testMoocApplicationContext : 1795799895
    }
    
}

<bean id="moocApplicationContext" class="com.rxy.aware.MoocApplicationContext" ></bean>

BeanNameAware

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class MoocBeanName implements BeanNameAware, ApplicationContextAware {

    private String beanName;
    
    @Override
    public void setBeanName(String name) {
        this.beanName = name;
        System.out.println("MoocBeanName : " + name);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        System.out.println("setApplicationContext : " + applicationContext.getBean(this.beanName).hashCode());
    }

}

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
    
    public TestAware() {
        super("classpath:spring-aware.xml");
    }   
    
    @Test
    public void textMoocBeanName() {
        System.out.println("textMoocBeanName : " + super.getBean("moocBeanName").hashCode());
        //MoocBeanName : moocBeanName
        //setApplicationContext : 1795799895
       //textMoocBeanName : 1795799895
    }
    
}

<bean id="moocBeanName" class="com.imooc.aware.MoocBeanName" ></bean>

注意:不同Aware接口执行顺序不同,如ApplicationContextAware的方法在BeanNameAware的方法执行之后,所以setBeanName中无法获取上下文对象

相关文章

  • Aware Mode

    Aware接口 Spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以...

  • [Spring]Spring的Aware接口-装配

    Aware Aware:察觉的、意识到的。英文示例: She was not aware of having d...

  • spring中Aware后缀

    aware: 意识到的;知道的; spring中带有Aware后缀的接口主要是和bean有关,实现了Aware后...

  • 20180718每日10个英文单词

    1.aware of 意识到 知道 Eg:We are aware of the potential proble...

  • 怎样用地道的英语表达“我懂了”?

    1. To be aware of I wasn't even aware that he was ill. 我都...

  • 一图看懂 spring bean 的生命周期

    注:容器关闭:容器指的是 ApplicationContext 容器。aware容器参数:aware 了解、明白之...

  • Aware

    package org.springframework.beans.factory 说明 使bean可以意识到自己...

  • Aware

    Aware 容器对 Bean 本身的逻辑基本是无侵入的,所以Bean一般不需要了解容器的状态和直接使用容器;但是有...

  • aware

    because the weather were so hot even still don't want t...

  • Spring之Aware

    Aware Aware提供了一种让用户实现了Aware接口的自定义bean能够感知到被spring管理的资源的能力...

网友评论

      本文标题:Aware Mode

      本文链接:https://www.haomeiwen.com/subject/khhtjqtx.html