1、原因
不知不覺已經從事java開發好幾年了,成了人們口中的老司機,但是一直都是恍恍惚惚過來,對于框架底層實作一直都沒有怎么了解過,只是在面試的時候背些面試題,慢慢地發現不能這樣,需要振作,笑~~~~,從這篇開始,記錄自己對于Spring加載程序的原始碼查看,
2、開始
1、在Spring加載bean并獲取bean定義的常用類是ClassPathXmlApplicationContext 以及AnnotationConfigApplicationContext,ClassPathXmlApplicationContext用來讀取并加載默認classpath 路徑下面的組態檔,AnnotationConfigApplicationContext是加載注解@Configuration標注的配置類,我現在關注于ClassPathXmlApplicationContext,
2、新建一個maven專案,并引入spring相關jar包,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除自帶logging,引入log4j2 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<!-- 排除自帶logging,引入log4j2 -->
</exclusions>
</dependency>
spring-boot-starter-we會依賴引入spring-context包,定義介面以及實作類
public interface Person {
void say();
}
@Log4j2
public class Teacher implements Person {
@Override
public void say() {
log.info("開始上課!");
}
}
撰寫bean.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" />
</beans>
開始撰寫測驗類用來測驗bean加載程序:
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
Teacher teacher = (Teacher) applicationContext.getBean("person");
teacher.say();
}
第一行打上斷點,debug開始跟著走,就會來到:
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
super(parent);
this.setConfigLocations(configLocations);
if (refresh) {
this.refresh();
}
}
點擊this.refresh()方法進入就來到了最核心的地方,這里弄懂了,bean加載就弄懂了:
public void refresh() throws BeansException, IllegalStateException {
synchronized(this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
this.prepareRefresh();
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
this.prepareBeanFactory(beanFactory);
try {
this.postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
this.invokeBeanFactoryPostProcessors(beanFactory);
this.registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
this.initMessageSource();
this.initApplicationEventMulticaster();
this.onRefresh();
this.registerListeners();
this.finishBeanFactoryInitialization(beanFactory);
this.finishRefresh();
} catch (BeansException var10) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var10);
}
this.destroyBeans();
this.cancelRefresh(var10);
throw var10;
} finally {
this.resetCommonCaches();
contextRefresh.end();
}
}
}
之后會慢慢開始學習這里!,新建公眾號:隨風的java開發人生,歡迎互關互助,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/510983.html
標籤:其他
