Interview questions - continuous update of spring FAQ and source code analysis

SteveCode. 2022-05-14 15:02:59 阅读数:111


IOC bean Implementation process and life cycle of

Write a first dome

 Insert picture description here
 Insert picture description here
catBean object

/** * Function description :bean object * * @author Songxianyang * @date 2022-05-13 15:42 */
public class CatBean {

private String name;


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<bean id="catBean" class="">
<property name="name" value=" Lolo "></property>


/** * Function description : Study Spring Source code * * @author Songxianyang * @date 2022-05-13 15:35 */
public class TestXmlSpring {

public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
CatBean cat = (CatBean) context.getBean("catBean");
} Method

setConfigLocations(configLocations); Obviously installed in our configuration file

 Insert picture description here

public void refresh() throws BeansException, IllegalStateException {

synchronized (this.startupShutdownMonitor) {

StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
// Prepare this context for refresh .
// Tell subclass to refresh inside bean factory . To get their own configuration in the project bean
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Get ready bean Factory to use... In this context . Prepare your own factory bean 
try {

// Allow for... In context subclasses bean After treatment in the factory .
StartupStep beanPostProcess = this.applicationStartup.start("");
// The call is registered in the context as bean The factory processor .
// Register to intercept bean Created bean processor .
// Initialize the message source for this context .
// Initialize the event multicast for this context 
// Initializes other special classes in a specific context subclass bean.
// Check the listener bean And register them .
// Instantiate all remaining ( Non lazy initialization ) Single case .
// The last step : Issue corresponding events .
catch (BeansException ex) {

if (logger.isWarnEnabled()) {

logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
// Destroy the created singleton to avoid hanging resources .
// Reset “ Activities ” sign .
// Propagate the exception to the caller .
throw ex;
finally {

// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
// Reset Spring Common introspective caching in the core , Because we may not need a single example anymore bean Metadata ......

Tell subclass to refresh inside bean factory . To get their own configuration in the project bean

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

 Insert picture description here
Then we'll follow in … Put it here first . Let's go from general to specific analysis

Get ready bean Factory to use... In this context . Prepare your own factory bean


It means : We have an example bean factory , But this beanFactory It's not something we can use . This method is for this beanFactory Up and down initialization . With these initializations, we can , Only the system environment can use this beanFactory.

What has been initialized ?

This method is relatively simple Let's talk about : Let's look at this first code
Jane knows what she means

/** * Configure the factory's standard context characteristics, * such as the context's ClassLoader and post-processors. * @param beanFactory the BeanFactory to configure * Standard context characteristics of configuration factory , For example, context ClassLoader And postprocessor . @param beanFactory To configure BeanFactory */
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {

// Tell the internal bean factory to use the context's class loader etc.
// Tell the inside bean Class loader for factory use context, etc .
// Get the default class loader In the bean factory 
if (!shouldIgnoreSpel) {

beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// Configure the bean factory with context callbacks.
// Use context callback configuration bean factory . To configure Environmental Science 、 Parser 、 information resources 、 Context 、 Class loader wait 
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
// BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
//BeanFactory The interface is not registered as a resolvable type in a normal factory . MessageSource As bean register ( And find... For automatic assembly ).
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.
// Will be used to detect internal bean The early post processor of is registered as ApplicationListener.
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// Detect a LoadTimeWeaver and prepare for weaving, if found.
// testing LoadTimeWeaver And prepare to weave ( If you find ).
if (!NativeDetector.inNativeImage() && 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.
// to bean Assign the default environment configuration 
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());