Sprng IOC 源码 debug

使用的是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()创建对象
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

讲述完毕
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值