Bean对IOC容器的感知(aware)

在某些情况我们需要对容器中的某些状态需要了解,所以Spring提供了相关的接口,通过特定的aware接口来完成。接口有:

  • 1)BeanNameAware。可以在bean中得到它在IOC容器中的Bean的实例名字
  • 2)BeanFactoryAware。可以再Bean中得到Bean所有的Ioc容器,从而直接在Bean中使用IOC容器的服务
  • 3)ApplicationContextAware。可以在Bean中获取到Bean所有的应用上下文,从而直接在bean中使用应用上下文的服务
  • 4)MessageSourceAware。在bean中可以得到消息源
  • 5)ApplicationEventPublisherAware,在bean中可以得到应用上下文的事件发布器,从而可以在bean中发布应用上下文的事件
  • 6)ResourceLoaderAware,在bean中可以得到ResourceLoader,从而在Bean中使用ResourceLoader加载外部对应的resource资源

  具体使用方法:直接实现这个接口,添加set方法即可。

   一般都是通过一个的xxxxProcessor(实现了BeanPostProcessor接口)的postProcessBeforeInitialization方法中对aware接口进行注入。例如ApplicationContextAwareProcessor源码:

class ApplicationContextAwareProcessor implements BeanPostProcessor {
    private final ConfigurableApplicationContext applicationContext;
    private final StringValueResolver embeddedValueResolver;

    public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
    }

    public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
        AccessControlContext acc = null;
        if (System.getSecurityManager() != null && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
            acc = this.applicationContext.getBeanFactory().getAccessControlContext();
        }

        if (acc != null) {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    ApplicationContextAwareProcessor.this.invokeAwareInterfaces(bean);
                    return null;
                }
            }, acc);
        } else {
            this.invokeAwareInterfaces(bean);
        }

        return bean;
    }

    private void invokeAwareInterfaces(Object bean) {
        if (bean instanceof Aware) {
            if (bean instanceof EnvironmentAware) {
                ((EnvironmentAware)bean).setEnvironment(this.applicationContext.getEnvironment());
            }

            if (bean instanceof EmbeddedValueResolverAware) {
                ((EmbeddedValueResolverAware)bean).setEmbeddedValueResolver(this.embeddedValueResolver);
            }

            if (bean instanceof ResourceLoaderAware) {
                ((ResourceLoaderAware)bean).setResourceLoader(this.applicationContext);
            }

            if (bean instanceof ApplicationEventPublisherAware) {
                ((ApplicationEventPublisherAware)bean).setApplicationEventPublisher(this.applicationContext);
            }

            if (bean instanceof MessageSourceAware) {
                ((MessageSourceAware)bean).setMessageSource(this.applicationContext);
            }

            if (bean instanceof ApplicationContextAware) {
                ((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);
            }
        }

    }

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        return bean;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40792878/article/details/82949784