使用的是spring boot
大体流程:
a. AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(DemoApplicationTests.class);
b. refresh()
c. finishBeanFactoryInitialization()
d. beanFactory.preInstantiateSingletons()
e. getBean()→doGetBean()
f. getSingleton()
g. singletonFactory.getObject()
h. createBean()→doCreateBean()
i. createBeanInstance()
j. instantiateBean()
k. instantiate()
l. instantiateClass()→newInstance()
基本配置:
Person.java
package com.example.demo.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component("person")
@ConfigurationProperties("person")
public class Person {
private String name;
private Integer age;
public Person() {
System.out.println("Person Constructor...");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
main方法
@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan("com.example.demo")
public class DemoApplicationTests {
@Test
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(DemoApplicationTests.class);
Person person = (Person) annotationConfigApplicationContext.getBean("person");
System.out.println(person);
}
}
大致目录(其实就是person和main方法):
开始debug
1. 先在mian方法的第一行打断点,并进入
2. 进入了annoationConfigApplicationContext的构造函数
this()会调用无参构造函数,register()注册bean配置类,refresh()刷新上下文。
而相关联的是第一个和第三个
this()方法
在这个类中可以发现annocationConfigApplicationContext继承了GenericApplicationContext类
而GenericApplicationContext类中,有个private final DefaultListableBeanFactory beanFactory;(即bean工厂)
在refresh()方法中,finishBeanFactoryInitialization()作用是初始化剩余的单例bean
进入finishBeanFactoryInitialization()方法
再进入preInstantiateSingletons()方法
这个方法中先遍历beanDefinition(存放bean的相关信息)的名字,找到目标对象(person)的的beanName后,进入getBean方法中
再进入getBean()方法
再进入doGetBean()方法
在doGetBean()方法中会调用两次getSingleton()方法
a. Object sharedInstance = getSingleton(beanName); 返回结果为null
b. 主要是这个方法
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
进入第二个getSingleton()方法
第一个圈中的singletonObjects是一个concurrentHashMap
而第二个圈中的singletonFactory需要返回上一步中getSingleton()方法中的createBean()方法,因为是lambda表达式
进入singletonFactory.getObject()方法后再进入createBean()方法
在createBean()方法中会进入doCreateBean()方法
进入doCreateBean()方法
这里会进入createBeanInstance()方法
进入createBeanInstance()方法
这里会进入instantiateBean()方法
进入instantiateBean()方法
进入instantiate()方法
这里constructorToUse指向目标对象的类的构造函数
进入instantiateClass()方法
这里通过newInstance()创建对象