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

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

interviewquestionscontinuousupdatespring

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 */
@Data
public class CatBean {

private String name;
}

spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="catBean" class="com.song.boot.springstudy.myspring.bean.CatBean">
<property name="name" value=" Lolo "></property>
</bean>
</beans>

TestXmlSpring

/** * 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(
"spring-config.xml");
CatBean cat = (CatBean) context.getBean("catBean");
System.out.println(cat.getName());
}
}

org.springframework.context.support.AbstractApplicationContext#refresh() 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 .
prepareRefresh();
// 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 
prepareBeanFactory(beanFactory);
try {

// Allow for... In context subclasses bean After treatment in the factory .
postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// The call is registered in the context as bean The factory processor .
invokeBeanFactoryPostProcessors(beanFactory);
// Register to intercept bean Created bean processor .
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// Initialize the message source for this context .
initMessageSource();
// Initialize the event multicast for this context 
initApplicationEventMulticaster();
// Initializes other special classes in a specific context subclass bean.
onRefresh();
// Check the listener bean And register them .
registerListeners();
// Instantiate all remaining ( Non lazy initialization ) Single case .
finishBeanFactoryInitialization(beanFactory);
// The last step : Issue corresponding events .
finishRefresh();
}
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 .
destroyBeans();
// Reset “ Activities ” sign .
cancelRefresh(ex);
// 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 ......
resetCommonCaches();
contextRefresh.end();
}
}
}

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

 prepareBeanFactory(beanFactory);

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 
beanFactory.setBeanClassLoader(getClassLoader());
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.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 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());
}
}
版权声明:本文为[SteveCode.]所创,转载请带上原文链接,感谢。 https://javamana.com/2022/134/202205141459440639.html