zoukankan      html  css  js  c++  java
  • spring IoC (八)Bean对IoC容器的感知

      容器管理的Bean一般不需要了解容器的状态和直接使用容器,但在某些情况下,是需要在Bean中直接

    对IoC容器进行操作的,这时候,就需要在Bean中设定对容器的感知。Spring IoC容器也提供了该功能,它

    是通过特定的aware接口来完成的。例如BeanNameAware、BeanFactorAware、ApplicationContextAware

    、MessageSourceAware、ResourceLoaderAware等。

      在 Bean实例化后,初始化前会调用BeanPostProcessor.postProcessBeforeInitialization

    AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization() -->

    BeanPostProcessor.postProcessBeforeInitialization(Object, String)

    例如ApplicationContextAwareProcessor中

     1     public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
     2         AccessControlContext acc = null;
     3 
     4         if (System.getSecurityManager() != null &&
     5                 (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
     6                         bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
     7                         bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
     8             acc = this.applicationContext.getBeanFactory().getAccessControlContext();
     9         }
    10 
    11         if (acc != null) {
    12             AccessController.doPrivileged(new PrivilegedAction<Object>() {
    13                 public Object run() {
    14                     invokeAwareInterfaces(bean);
    15                     return null;
    16                 }
    17             }, acc);
    18         }
    19         else {
    20             invokeAwareInterfaces(bean);
    21         }
    22 
    23         return bean;
    24     }
    25 
    26     private void invokeAwareInterfaces(Object bean) {
    27         if (bean instanceof Aware) {
    28             if (bean instanceof EnvironmentAware) {
    29                 ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
    30             }
    31             if (bean instanceof EmbeddedValueResolverAware) {
    32                 ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(
    33                         new EmbeddedValueResolver(this.applicationContext.getBeanFactory()));
    34             }
    35             if (bean instanceof ResourceLoaderAware) {
    36                 ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
    37             }
    38             if (bean instanceof ApplicationEventPublisherAware) {
    39                 ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
    40             }
    41             if (bean instanceof MessageSourceAware) {
    42                 ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
    43             }
    44             if (bean instanceof ApplicationContextAware) {
    45                 ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
    46             }
    47         }
    48     }
  • 相关阅读:
    Instant Python 中文缩减版
    《Java疯狂讲义》(第3版)学习笔记 2
    《Java疯狂讲义》(第3版)学习笔记 1
    NXP Mifare S50标准IC卡- 访问位(Access Bits) 分析
    Python中获取异常(Exception)信息
    支持向量机(SVM)入门
    棋类游戏中人机博弈的设计
    (翻译)如何对python dict 类型按键(keys)或值(values)排序
    Python实现打印二叉树某一层的所有节点
    FIFA halts 2026 bids amid scandal 国际足联在丑闻期间停止2026年足球世界杯申请
  • 原文地址:https://www.cnblogs.com/toUpdating/p/9729538.html
Copyright © 2011-2022 走看看