Spring Aware接口解析

1.Aware接口简介

Spring的依赖注入的最大亮点是所有的Bean对Spring容器的存在是没有意识的,我们可以将Spring容器换成其他的容器,Spring容器中的Bean的耦合度因此也是极低的。
但是我们在实际的开发中,我们却经常要用到Spring容器本身的功能资源,所以Spring容器中的Bean此时就要意识到Spring容器的存在才能调用Spring所提供的资源。我们通过Spring提供的一系列接口Spring Aware来实现具体的功能。

/**
 * A marker superinterface indicating that a bean is eligible to be notified by the
 * Spring container of a particular framework object through a callback-style method.
 * The actual method signature is determined by individual subinterfaces but should
 * typically consist of just one void-returning method that accepts a single argument.
 *
 * <p>Note that merely implementing {@link Aware} provides no default functionality.
 * Rather, processing must be done explicitly, for example in a
 * {@link org.springframework.beans.factory.config.BeanPostProcessor}.
 * Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}
 * for an example of processing specific {@code *Aware} interface callbacks.
 *
 * @author Chris Beams
 * @author Juergen Hoeller
 * @since 3.1
 */
public interface Aware {

}

Aware是一个具有标识作用的超级接口,实现该接口的bean是具有被spring 容器通知的能力的,而被通知的方式就是通过回调。也就是说:直接或间接实现了这个接口的类,都具有被spring容器通知的能力。

常见Aware的作用

  • BeanNameAware 获得到容器中Bean的名称
  • BeanFactoryAware 获得当前bean Factory,从而调用容器的服务
  • ApplicationContextAware 获得当前的application context从而调用容器的服务
  • MessageSourceAware 得到message source从而得到文本信息
  • ApplicationEventPublisherAware 应用时间发布器,用于发布事件
  • ResourceLoaderAware 获取资源加载器,可以获得外部资源文件

Spring Aware的目的是为了让Bean获得Spring容器的服务。因为ApplicationContext接口集成了MessageSource接口、ApplicationEventPublisher接口和ResourceLoader接口,因此当Bean继承自ApplicationContextAware的时候就可以得到Spring容器的所有服务。

  • 2.实例演示——ApplicationContextAware
@Component
public class Red implements ApplicationContextAware, BeanNameAware , EmbeddedValueResolverAware {
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
        System.out.println("传入的IOC容器:"+applicationContext);
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("当前bean的名字:"+name);
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        String s = resolver.resolveStringValue("您好 ${os.name} 我是 #{10*12}");
        System.out.println("解析的字符串是 :"+s);
    }
}
  • 调用setApplicationContext的调用栈
    是在applyBeanPostProcessorsBeforeInitialization中后置处理器ApplicationContextAwareProcessor调用Plane.setApplicationContext的回调函数实现的

在这里插入图片描述
在这里插入图片描述

一、BeanNameAware是在invokeAwareMethods中进行回调函数调用

在这里插入图片描述
在这里插入图片描述

二、ApplicationContextAware和EmbeddedValueResolverAware在后置处理器中调用

这两个都是在后置处理器ApplicationContextAwareProcessor中进行回调函数调用的。
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值