美文网首页spring 相关
Spring容器(三):prepare BeanFactory

Spring容器(三):prepare BeanFactory

作者: aix91 | 来源:发表于2019-01-13 15:02 被阅读0次

    在BeanFactory的准备阶段,会调用下面几个方法:

    • prepareRefresh();
    • ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    • prepareBeanFactory(beanFactory);
    • postProcessBeanFactory(beanFactory);
    1. prepareRefresh()
    protected void prepareRefresh() {
            //初始化属性设置;子类可以自定义自己的属性
            initPropertySources();
            //属性校验
            getEnvironment().validateRequiredProperties();
            this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>();
        }
    

    2. obtainFreshBeanFactory()

        protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
            //为上文中创建好的DefaultListableBeanFactory设置SerializationId
            refreshBeanFactory();
            //返回创建的BeanFactory
            ConfigurableListableBeanFactory beanFactory = getBeanFactory();
            if (logger.isDebugEnabled()) {
                logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
            }
            return beanFactory;
        }
    

    3. prepareBeanFactory(beanFactory):给beanFactory设置一些属性:环境、系统属性

    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.getBeanClassLoader()));
           beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
    
           // 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.ignoreDependencyInterface(EnvironmentAware.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.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
               beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
           }
           if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
               beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
           }
           if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
               beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
           }
       }
    

    4. postProcessBeanFactory(beanFactory)

    子类可以通过重写这个方法,在BeanFactory准备完成阶段做进一步的设置

    相关文章

      网友评论

        本文标题:Spring容器(三):prepare BeanFactory

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