Spring 的那点事 (四) —— prepareBeanFactory(beanFactory)

185 阅读3分钟

这是我参与11月更文挑战的第5天,活动详情查看:2021最后一次更文挑战

作用

本方法用于设置 beanFactory 的类加器、SpEL解析器、bean的属性编辑器、特殊接口的免自动注入和特殊类型的指定注入.对环境相关的对象 environment、systemProperties、systemEnvironment 设置为 beanFactory 单例.

prepareBeanFactory 源码

本方法内部没有太多复杂的逻辑, 主要做了一些准备工作, 具体看注释吧

/**
 * Configure the factory's standard context characteristics,
 * such as the context's ClassLoader and post-processors.
 *
 * @param beanFactory the BeanFactory to configure
 */
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
   // Tell the internal bean factory to use the context's class loader etc.
   // 设置 beanFactory 的 classLoader 为当前 context 的 classLoader
   beanFactory.setBeanClassLoader(getClassLoader());
   if (!shouldIgnoreSpel) {
      // 设置 EL 表达式解析器 (Bean 初始化完成后填充属性时会用到)
      // spring3 增加了表达式语言支持,默认可以使用 #{bean.xxx} 的形式来调用相关属性值
      beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
   }
   // 设置属性注册解析器 PropertyEditor, 这个主要是对 bean 的属性等设置管理的一个工具
   beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

   // Configure the bean factory with context callbacks.
   /*
    * 将当前的 ApplicationContext 对象交给 ApplicationContextAwareProcessor 类来处理,从而在 Aware 接口实现类中注入 applicationContext 等
    * 添加了一个处理 Aware 相关接口的 beanPostProcessor 扩展,主要是使用 beanPostProcessor 的 postProcessBeforeInitialization() 前置处理方法实现 Aware 相关接口的功能
    * 类似的还有 ResourceLoaderAware、ServletContextAware 等
    */
   beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
   /*
    * 下面是忽略的自动装配(也就是实现了这些接口的 Bean,不要 Autowired 自动装配了)
    * 默认只有 BeanFactoryAware 被忽略,所以其它的需要自行设置
    * 因为 ApplicationContextAwareProcessor 把这 5 个接口的实现工作做了,所以这里就直接忽略掉
    */
   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.ignoreDependencyInterface(ApplicationStartupAware.class);

   // BeanFactory interface not registered as resolvable type in a plain factory.
   // MessageSource registered (and found for autowiring) as a bean.
   /*
    * 设置几个"自动装配"规则,如下:
    * 如果是 BeanFactory 的类,就注册 beanFactory
    * 如果是 ResourceLoader、ApplicationEventPublisher、ApplicationContext 等,就注入当前对象 this(applicationContext对象)
    * 此处 registerResolvableDependency() 会把它们加入到 DefaultListableBeanFactory 的 resolvableDependencies 字段里面缓存,供后面处理依赖注入的时候使用,DefaultListableBeanFactory#resolveDependency 处理依赖关系
    * 这也是为什么我们可以通过依赖注入的方式直接注入这几个对象,比如 ApplicationContext 可以直接依赖注入
    * 但是需要注意的是:这些 Bean,Spring 的 IOC 容器里其实是没有的。beanFactory.getBeanDefinitionNames() 和 beanFactory.getSingletonNames() 都是找不到它们的,特别需要理解这一点
    */
   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.
   /*
    * 注册这个 Bean 的后置处理器:在 Bean 初始化后检查是否实现了 ApplicationListener 接口
    * 实现了则加入当前 applicationContext 的 applicationListeners 列表,这样后面广播事件也就方便了
    */
   beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

   // Detect a LoadTimeWeaver and prepare for weaving, if found.
   /*
    * 检查容器中是否包含名称为 loadTimeWeaver 的 bean,实际上是增加 Aspectj 的支持
    * AspectJ 采用编译期织入、类加载期织入两种方式进行切面的织入
    * 类加载期织入简称为 LTW(Load Time Weaving),通过特殊的类加载器来代理 JVM 默认的类加载器实现
    */
   if (!NativeDetector.inNativeImage() && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
      /*
       * 添加 Bean 后置处理器:LoadTimeWeaverAwareProcessor
       * 在 Bean 初始化之前检查 Bean 是否实现了 LoadTimeWeaverAware 接口,如果是,则进行加载时织入,即静态代理。
       */
      beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
      // Set a temporary ClassLoader for type matching.
      beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
   }

   // Register default environment beans.
   // 注入一些其它信息的 bean,比如 environment、systemProperties、SystemEnvironment 等
   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());
   }
   if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {
      beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
   }
}