关于java:Spring源码之四prepareBeanFactory方法

9次阅读

共计 3489 个字符,预计需要花费 9 分钟才能阅读完成。

Spring 源码之四 prepareBeanFactory() 办法

大家好,我是程序员田同学!

明天带大家解读 refresh() 办法的第三个办法 prepareBeanFactory(),通过对 refresh() 的一步步解读,想必有一天小伙伴们能揭开 Spring 的神秘面纱。

照例,先站在 prepareBeanFactory() 的门外看它都做了什么,而后再逐渐深刻分析。prepareBeanFactory() 办法次要是:

设置 BeanFactory 的类加载器,增加 BeanPostProcessor,手动注册几个非凡的 bean。

扩大:BeanPostProcessor 是 Spring IOC 容器给咱们提供的一个扩大接口。

//bean 初始化办法调用前被调用
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}
    
    //bean 初始化办法调用后被调用
    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}

BeanPostProcessor 在整个 bean 的生命周期中处于红色箭头所示的地位。

prepareBeanFactory() 的每个具体办法在以下代码正文中。

    // Tell the internal bean factory to use the context's class loader etc.
        // 设置上下文环境的启动类加载器
        beanFactory.setBeanClassLoader(getClassLoader());

        // 增加 bean 表达式解释器,为了可能让咱们的 beanFactory 去解析 bean 表达式
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));

        //spring 外部的属性编辑器、既读取配置文件
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

        // Configure the bean factory with context callbacks.
        // 增加 BeanPostProcessor
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

        // 跳过以下 6 个属性的主动注入
        // 因为在 ApplicationContextAwareProcessor 后置处理器中通过 setter 注入
        beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
        beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
        beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
        beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

        // BeanFactory interface not registered as resolvable type in a plain factory.
        // MessageSource registered (and found for autowiring) as a bean.

        //// 注册四个非凡的 bean
        // 在利用代码就能够通过类型主动拆卸把工厂实例和 ApplicationContext 实例设置到自定义 bean 的属性中
        // 这四个属性都会被主动设置,尽管没有在显示的在 bean 定义 xml 中注入它们
        beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
        beanFactory.registerResolvableDependency(ResourceLoader.class, this);
        beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
        beanFactory.registerResolvableDependency(ApplicationContext.class, this);

        // Register early post-processor for detecting inner beans as ApplicationListeners.
        // 注册事件监听器
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));


        // Detect a LoadTimeWeaver and prepare for weaving, if found.
        if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            // Set a temporary ClassLoader for type matching.
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }

        // Register default environment beans.
        // 如果没有定义 "environment" 这个 bean,那么 Spring 会 "手动" 注册一个
        if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
        }
        // 如果没有定义 "systemProperties" 这个 bean,那么 Spring 会 "手动" 注册一个
        if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
        }
        // 如果没有定义 "systemEnvironment" 这个 bean,那么 Spring 会 "手动" 注册一个
        if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
        }

有一些同学想必对 ignoreDependencyInterface() 办法有些许的纳闷。

ignoreDependencyInterface 的次要性能是疏忽给定接口的主动拆卸性能,也就是当有疏忽的接口类,主动拆卸会疏忽这部分类的初始化拆卸,因为某种状况下,此时的接口实现类不能初始化。

例如 BeanNameAware, 要想拆卸这个接口的实现对象,能够实现这个接口,通过实现的 set 办法进行拆卸

正文完
 0