Spring Aware接口解析

  作者:记性不好的阁主

链接:https://www.jianshu.com/p/5865c5c3d0a3


1.Aware接口简介


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


/**
 * 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. 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 BeanPostProcessor}.
 * Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}
 * and {@link org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory}
 * for examples of processing {@code *Aware} interface callbacks.
 *
 * @author Chris Beams
 * @since 3.1
 */
public interface Aware {

}


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

  • 常见的Aware

  • 常见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


实现 ApplicationContextAware 接口的Plane.java


@Component
public class Plane implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    public Plane() {
        System.out.println("Plane.....constructor........");
    }

    @PostConstruct
    public void init(){
        System.out.println("Plane.....@PostConstruct........");
    }

    @PreDestroy
    public void destory(){
        System.out.println("Plane.....@PreDestroy......");
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //将applicationContext传进来,可以拿到
        this.applicationContext = applicationContext;
    }
}


    • 调用Plane.setApplicationContext的调用栈
      是在applyBeanPostProcessorsBeforeInitialization中后置处理器ApplicationContextAwareProcessor调用Plane.setApplicationContext的回调函数实现的。





    3.实例演示——ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware


    参考Light.java


    @Component
    public class Light implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
        private ApplicationContext applicationContext;
    
        @Override
        public void setBeanName(String name) {
            System.out.println("当前bean的名字:"+name);
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            System.out.println("传入的IOC容器: "+applicationContext);
            this.applicationContext = applicationContext;
        }
    
        @Override
        public void setEmbeddedValueResolver(StringValueResolver resolver) {
            String result = resolver.resolveStringValue("你好${os.name}, 计算#{3*8}");
            System.out.println("解析的字符串为---"+result);
        }
    }
    


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





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

    这两个都是在后置处理器ApplicationContextAwareProcessor中进行回调函数调用的。








    作者:王侦
    链接:https://www.jianshu.com/p/5865c5c3d0a3
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。






    相关推荐

    评论 抢沙发

    表情

    分类选择