Spring 容器初始化源码跟读refresh03

本文详细解析了Spring框架中Context启动过程中的prepareBeanFactory方法,包括设置类加载器、注册表达式解析器、添加默认PropertyEditor、配置BeanPostProcessor、设置依赖注入规则等关键步骤。

前面两篇文章写到了refresh方法的

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

根据配置文件将配置的类加载到内存中(bean定义)并返回了默认的beanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory)实例

接下来是

    // Prepare the bean factory for use in this context.
    prepareBeanFactory(beanFactory);

这里主要是设置了spring在运行时需要使用的一些属性

具体代码

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        // Tell the internal bean factory to use the context's class loader etc.
        beanFactory.setBeanClassLoader(getClassLoader());
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));

        // Configure the bean factory with context callbacks.
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
        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.
        beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
        beanFactory.registerResolvableDependency(ResourceLoader.class, this);
        beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
        beanFactory.registerResolvableDependency(ApplicationContext.class, 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.
        if (!beanFactory.containsBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
            Map systemProperties;
            try {
                systemProperties = System.getProperties();
            }
            catch (AccessControlException ex) {
                systemProperties = new ReadOnlySystemAttributesMap() {
                    @Override
                    protected String getSystemAttribute(String propertyName) {
                        try {
                            return System.getProperty(propertyName);
                        }
                        catch (AccessControlException ex) {
                            if (logger.isInfoEnabled()) {
                                logger.info("Not allowed to obtain system property [" + propertyName + "]: " +
                                        ex.getMessage());
                            }
                            return null;
                        }
                    }
                };
            }
            beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, systemProperties);
        }

        if (!beanFactory.containsBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
            Map<String,String> systemEnvironment;
            try {
                systemEnvironment = System.getenv();
            }
            catch (AccessControlException ex) {
                systemEnvironment = new ReadOnlySystemAttributesMap() {
                    @Override
                    protected String getSystemAttribute(String variableName) {
                        try {
                            return System.getenv(variableName);
                        }
                        catch (AccessControlException ex) {
                            if (logger.isInfoEnabled()) {
                                logger.info("Not allowed to obtain system environment variable [" + variableName + "]: " +
                                        ex.getMessage());
                            }
                            return null;
                        }
                    }
                };
            }
            beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, systemEnvironment);
        }
    }

详细解析:

1、设置bean的自定义类加载器 

2、注册表达式解析器,这里是:StandardBeanExpressionResolver

spring3增加了表达式语言的支持,默认可以使用#{bean.xxx}的形式来调用相关属性值。

3、为beanFactory增加了一个默认的propertyEditor,这个主要是对bean的属性等设置管理的一个工具

4、第4行添加了一个处理aware相关接口的beanPostProcessor扩展,主要是使用beanPostProcessor的postProcessBeforeInitialization()前置处理方法实现aware相关接口的功能,aware接口是用来给bean注入一些资源的接口,例如实现BeanFactoryAware的Bean在初始化后,Spring容器将会注入BeanFactory的实例相应的还有ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等。

5、第5-8行设置了几个忽略自动装配的接口,默认只有BeanFactoryAware被忽略,其他的都要自行设置,这里设置了ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware和ApplicationContextAware。

6、第9-12行设置了几个自动装配的特殊规则,如果是BeanFactory类型,则注入beanFactory对象,如果是ResourceLoader、ApplicationEventPublisher、ApplicationContext类型则注入当前对象(applicationContext对象)。

7、这部分判断是否定义了名为loadTimeWeaver的bean,如果定义了则添加loadTimeWeaver功能的beanPostProcessor扩展,并且创建一个临时的classLoader来让其处理真正的bean

8、这部分首先判断是否定义了名为systemProperties的bean,如果没有则加载系统获取当前系统属性System.getProperties()并注册为一个单例bean。假如有AccessControlException权限异常则创建一个ReadOnlySystemAttributesMap对象,可以看到创建时重写了getSystemAttribute()方法,查看ReadOnlySystemAttributesMap的代码可以得知在调用get方法的时候会去调用这个方法来获取key对应的对象,当获取依旧有权限异常AccessControlException的时候则返回null

9、这部分和上面一部分类似,只不过由系统属性改为了系统环境变量,异常处理方式等也和上面一部分一样。这两部分都是为spring内部提供系统信息的支撑bean

 

参考:https://www.cnblogs.com/tangpu/p/3203895.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值