美文网首页
org.springframework.beans.factor

org.springframework.beans.factor

作者: moralok | 来源:发表于2020-04-15 18:24 被阅读0次
    package org.springframework.beans.factory;
    
    /**
     * 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 的含义

    察觉、注意到、知道的。

    Aware 接口文档

    一个标记父接口,指示了一个 Bean 有资格被 Spring 容器通过一个回调风格的方法通知一个特定的框架对象。实际的方法签名是由各个子接口确定的,但通常应该仅包含一个接受单个参数、返回值为 void 的方法。

    请注意,仅仅实现 Aware 接口不会提供默认功能。相反,处理必须明确地进行,例如在一个 org.springframework.beans.factory.config.BeanPostProcessor 中,请参阅 org.springframework.context.support.ApplicationContextAwareProcessor 中有关处理具体的 Aware 接口回调的示例。

    例子

    参考文档中提到的 ApplicationContextAwareProcessor,它实现了BeanPostProcessor接口。

    @Override
    @Nullable
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
                bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
                bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)){
            return bean;
        }
    
        AccessControlContext acc = null;
    
        if (System.getSecurityManager() != null) {
            acc = this.applicationContext.getBeanFactory().getAccessControlContext();
        }
    
        if (acc != null) {
            AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                invokeAwareInterfaces(bean);
                return null;
            }, acc);
        }
        else {
            invokeAwareInterfaces(bean);
        }
    
        return bean;
    }
    
    private void invokeAwareInterfaces(Object bean) {
        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);
        }
    }
    

    当它调用 postProcessBeforeInitialization 方法时,会调用 invokeAwareInterfaces 方法。如果 Bean 实现了 Aware 的一个子接口,那么就会调用对应子接口中的方法。调用方法时,可能会传入一个参数,就是文档中提到的框架对象,如 applicationContext

    相关文章

      网友评论

          本文标题:org.springframework.beans.factor

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