1. 程式人生 > 實用技巧 >Spring擴充套件之五:Aware介面等

Spring擴充套件之五:Aware介面等

ApplicationContextAwareProcessor

1.介紹

ApplicationContextAwareProcessor是一個Spring內部工具,它實現了介面BeanPostProcessor,用於向實現瞭如下某種Aware介面的bean注入ApplicationContext中相應的屬性:

  • EnvironmentAware

  • EmbeddedValueResolverAware

  • ResourceLoaderAware

  • ApplicationEventPublisherAware

  • MessageSourceAware

  • ApplicationContextAware

如果bean實現了以上介面中的某幾個,那麼這些介面方法呼叫的先後順序就是上面介面排列的先後順序。

EnvironmentAware、ResourceLoaderAware、ApplicationContextAware等

1.介紹

Spring框架預留的關鍵的介面,Spring容器會自動把Environment、ResourceLoader、ApplicationContext等物件通過set...()方法注入。

2.使用

@Component
public final class ApplicationContextUtil implements ApplicationContextAware {

    
private static ApplicationContext applicationContext; private static Environment environment; // 通過set方法注入Environment @Override public void setEnvironment(Environment environment) { this.environment = environment; } // 通過set方法注入ApplicationContext @Override public
void setApplicationContext(ApplicationContext contex) throws BeansException { this.applicationContext = contex; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Environment getEnvironment() { return environment; } }

3.觸發點

ApplicationContextAwareProcessor類下:
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);
    }
}

4.順序圖

BeanNameAware、BeanClassLoaderAware、BeanFactoryAware

1.介紹

Spring框架預留的關鍵的介面,獲取當前Bean的名稱,獲取當前Bean的類裝載器和bean工廠。

如果bean實現了以上介面中的某幾個,那麼這些介面方法呼叫的先後順序就是上面排列的先後順序

2.觸發點

AbstractAutowireCapableBeanFactory類下:

private void invokeAwareMethods(String beanName, Object bean) {
    if (bean instanceof Aware) {
        if (bean instanceof BeanNameAware) {
            ((BeanNameAware) bean).setBeanName(beanName);
        }
        if (bean instanceof BeanClassLoaderAware) {
            ClassLoader bcl = getBeanClassLoader();
            if (bcl != null) {
                ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
            }
        }
        if (bean instanceof BeanFactoryAware) {
            ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
        }
    }
}

@PostConstruct