preface

According to the refresh process, when the obtainFreshBeanFactory execution is complete, the next step is to prepare the BeanFactory. As the name implies, this method mainly prepares the BeanFactory. Let’s take a look at this part of the logic.

prepareBeanFactory

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    // Tell the internal bean factory to use the context's class loader etc.
    // Set the beanFactory classloader
    beanFactory.setBeanClassLoader(getClassLoader());
    // The spring.spel.ignore property controls whether an SPEL expression is parsed
    if(! shouldIgnoreSpel) { beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    }
    // Set the property resolver
    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

    // Configure the bean factory with context callbacks.
    / / added to the list of post processor, the newly created ApplicationContextAwareProcessor into the reference for the current ApplicationContext
    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

    // Ignore autowiring
    // Only BeanFactoryAware is ignored by default to ignore other types, you need to set it separately
    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(ApplicationStartup.class);

    // BeanFactory interface not registered as resolvable type in a plain factory.
    // MessageSource registered (and found for autowiring) as a bean.
    // Register classes for autowiring
    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.
    // Whether to add Aspectj support for weaving during class loading
    if(! IN_NATIVE_IMAGE && 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.
    // Register other 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()); }if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {
        beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
    }
}
Copy the code

This piece of code is longer, but the logic is relatively simple, directly paste the code.

Then the code analysis mainly goes through the following stages:

  1. AddBeanPostProcessor add BeanPostProcessor
  2. RegisterResolvableDependency register dependencies
  3. RegisterSingleton registers the other singleton beans

Debug can check it below.

Debug

Before method call:

After registerResolvableDependency perform

Here found call registerResolvableDependency execution after the beanDefinitionNames did not add more related objects.

Add it to the Map resolvableDependencies.

! []

review

Said, when it introduced DefaultListableBeanFactory BeanDefinition is stored in the beanDefinitionMap. The dependencies are stored in resolvableDependencies.

postProcessBeanFactory

After prepareBeanFactory is executed, when you see postProcessBeanFactory(beanFactory); Method is very confused, because this is the need to subclass implementation, just as a template method, after the subclass implementation, you can add your own logic in it.

conclusion

This article is relatively simple: prepare a BeanFactory to which you can add system dependencies and beans, while postProcessBeanFactory is a template method to be implemented by subclasses.

Related to recommend

  • Spring source learning 09: Refresh about the process
  • Spring source code 08: Register the configuration class
  • Spring source learning 07: ClassPathBeanDefinitionScanner