阅读源码了解Spring启动流程

879 阅读1分钟

Spring启动流程

总结

image-20210411145037024

1.从run方法开始

spring的配置要从这个@SpringBootApplication这个注解里获取

image-20210410214738475

2.进入run方法

image-20210410215018280

image-20210410215157700

3.进入SpringApplication方法

看看准备了什么样的环境

image-20210410215704048

4.点进getSpringFactoriesInstance

image-20210410220323398

5.点进loadFactoryNames

一直往下,点到

image-20210410220941132

image-20210410221547106

image-20210410221734406

这一步是准备了很多类

image-20210411094617453

6.回到第三步

image-20210410222141152

进入getSpringFactoriesInstance看看是不是从缓存里取了

image-20210410222409204

image-20210410222625736

到这里环境就准备完了

image-20210411095044920

7.回到第二步,进入run方法

image-20210410222933909

8.

image-20210411101859360

看看初始化后的环境是什么样子的

image-20210411102234903

9.

image-20210411102837742

10.进入refreshContext

往里面多走几步能找到refresh方法

image-20210411103208847

11.点进prepareRefresh

image-20210411103939560

12.回到10

image-20210411104348487

13.点进prepareBeanFactory

image-20210411104933186

14.回到12

image-20210411110210350

image-20210411111413061

image-20210411111902079

15.进入finishBeanFactoryInitialization

image-20210411112105287

16.进入preInstantiateSingletons

image-20210411112314270

image-20210411112603619

17.进入getBean,往下啊进

image-20210411113004177

18.进入createBean

image-20210411113144721

19.进入doCreateBean

image-20210411113801153

自己写一个包装类

package com.example.studysource.test;

import java.lang.reflect.Field;

/**
 * @author SJ
 * @date 2021/4/11
 */
//对象包装类
public class ObjWrapper {
    //持有一个对象
    private Object object;

    public Object getObject() {
        return object;
    }

    //对外暴露两个方法
    public void setObject(Object object) {
        this.object = object;
    }


    public void setProp(String key,String value)throws Exception{

        //拿到名字为key的私有字段  拿公有字段是getField
        Field key1 = object.getClass().getDeclaredField(key);
        //暴力注入
        key1.setAccessible(true);
        key1.set(object,value);

    }

    public static void main(String[] args) throws Exception{
        User user = new User();
        ObjWrapper objWrapper = new ObjWrapper();
        objWrapper.setObject(user);
        objWrapper.setProp("username","zhangsan");
        System.out.println(objWrapper.getObject());

    }

}

image-20210411122121163

20.进入createBeanInstance

image-20210411122846305

21.进入instantiateBean

image-20210411123020985

22.进入instantiate

image-20210411123219609

23.进入instantiateClass

image-20210411123537865

24.回到14

完成Bean工厂初始化

25.回到9

完成refreshContext

控制台日志报错

image-20210411130228145

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
 

是因为我们自动装配了mybatis,但没有配数据源,配一下就行。