Spring Bean生命周期-prepareBeanFactory(五)

在ApplicationContext刷新获取beanFactory之后,开始准备context使用的beanFactory。这一步相对比较简单,就是配置facotry的特性,比如:ClassLoader,post-processors等等

代码如下:

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

        // Configure the bean factory with context callbacks.
        // 配置bean factory的context回调
        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.
        // 注册默认的environment bean
        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());
        }
    }

分析

beanfactory的准备阶段,比较简单,主要明白准备阶段操作的几个方法。

beanFactory.addBeanPostProcessor

添加BeanPostProcessor,当bean被这个工厂创建的时候会用到PostProcessor, 在beanfactory中存储了beanPostProcessors的列表,在生效的时候,列表中的PostProcessor都会执行。

        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(Class<?> ifc)

在注入的时候忽略此方法指定的接口类。也就是指定的接口不会被注入进去。

如下,不会生效

@Autowire 
ResourceLoaderAware aware;
        beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
        beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
        beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.registerResolvableDependency(Class<?> dependencyType, Object autowiredValue)

注册可以解析的依赖关系,当注入的类型为dependencyType的时候,注入autowiredValue。
注入类型与注入值的关系存储在map中。

resolvableDependencies = new ConcurrentHashMap<Class<?>, Object>(16);

// 注入类型为BeanFactory时,注入beanFactory(DefaultListableBeanFactory)
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
        beanFactory.registerResolvableDependency(ResourceLoader.class, this);

//注入类型为ApplicationEventPublisher时,注入值为this(ApplicationContext)
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);

//注入类型为ApplicationContext时,注入值也为this(ApplicationContext)
beanFactory.registerResolvableDependency(ApplicationContext.class, this);

额外

prepareBeanFactory主要做一些准备工作,没有什么逻辑,在Applicationcontext的refresh方法中往下看一步,执行

postProcessBeanFactory(beanFactory);

这一步Spring框架中没有实现,留待我们自己实现。

最后

这次看了refresh方法中的prepareBeanFactory内容比较简单,就不多说了。

上一篇:贾扬清演讲实录:一个AI开发者的奇幻漂流


下一篇:SpringBoot整合FreeMarker 报错 FTL stack trace ("~" means nesting-related): - Failed at: ${ctx