springboot启动过程

本文详细剖析了SpringApplication的构造过程和run方法的执行步骤,包括BeanDefinition的获取、应用类型的判断、监听器的记录、主启动类的确定以及Spring容器的初始化。此外,还探讨了环境变量的处理、配置属性的绑定、启动事件的发布和容器的创建等关键环节,深入理解Spring Boot的启动机制。
摘要由CSDN通过智能技术生成

SpringApplication构造分析

1、记录 BeanDefinition 源

spring容器刚开始是空的,要去各个源找到beanDefinition,这些源可能是配置类,可能是xml文件。在构造方法里会获取一个主源,也就是引导类,根据引导类去获取beanDefinition。

2、推断应用类型

根据jar包去判断是什么引用类型

3、记录 ApplicationContext 初始化器

对ApplicationContext做扩展

4、记录监听器

监听重要事件

5、推断主启动类  

记录运行的主类。

SpringApplication run分析

1、得到 SpringApplicationRunListeners,名字取得不好,实际是事件发布器

  • 发布 application starting 事件1️⃣,在程序启动的重要节点发布事件

        public static void main(String[] args) throws Exception{
    
            // 添加 app 监听器
            SpringApplication app = new SpringApplication();
            app.addListeners(e -> System.out.println(e.getClass()));
    
            // 获取事件发送器实现类名
            List<String> names = SpringFactoriesLoader.loadFactoryNames(SpringApplicationRunListener.class, A39_2.class.getClassLoader());
            for (String name : names) {
                System.out.println(name);
                Class<?> clazz = Class.forName(name);
                Constructor<?> constructor = clazz.getConstructor(SpringApplication.class, String[].class);
                SpringApplicationRunListener publisher = (SpringApplicationRunListener) constructor.newInstance(app, args);
    
                // 发布事件
                DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
                publisher.starting(bootstrapContext); // spring boot 开始启动
                publisher.environmentPrepared(bootstrapContext, new StandardEnvironment()); // 环境信息准备完毕
                GenericApplicationContext context = new GenericApplicationContext();
                publisher.contextPrepared(context); // 在 spring 容器创建,并调用初始化器之后,发送此事件
                publisher.contextLoaded(context); // 所有 bean definition 加载完毕
                context.refresh();
                publisher.started(context); // spring 容器初始化完成(refresh 方法调用完毕)
                publisher.running(context); // spring boot 启动完毕
    
                publisher.failed(context, new Exception("出错了")); // spring boot 启动出错
            }
    
        }

 2、封装启动 args

3、准备 Environment 添加命令行参数(*)

public static void main(String[] args) throws IOException {
        ApplicationEnvironment env = new ApplicationEnvironment(); // 系统环境变量, properties, yaml
        env.getPropertySources().addLast(new ResourcePropertySource(new ClassPathResource("step3.properties")));
        env.getPropertySources().addFirst(new SimpleCommandLinePropertySource(args));
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        }
//        System.out.println(env.getProperty("JAVA_HOME"));

        System.out.println(env.getProperty("server.port"));
    }

4、ConfigurationPropertySources 处理(*)

  • 发布 application environment 已准备事件2️⃣

        public static void main(String[] args) throws IOException, NoSuchFieldException {
            ApplicationEnvironment env = new ApplicationEnvironment();
            env.getPropertySources().addLast(
                    new ResourcePropertySource("step4", new ClassPathResource("step4.properties"))
            );
            ConfigurationPropertySources.attach(env);
            for (PropertySource<?> ps : env.getPropertySources()) {
                System.out.println(ps);
            }
    
            System.out.println(env.getProperty("user.first-name"));
            System.out.println(env.getProperty("user.middle-name"));
            System.out.println(env.getProperty("user.last-name"));
        }
    }

5、通过 EnvironmentPostProcessorApplicationListener 进行 env 后处理(*)

  • application.properties,由 StandardConfigDataLocationResolver 解析

  • spring.application.json

    public class Step5 {
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication();
            app.addListeners(new EnvironmentPostProcessorApplicationListener());
    
            /*List<String> names = SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class, Step5.class.getClassLoader());
            for (String name : names) {
                System.out.println(name);
            }*/
    
            EventPublishingRunListener publisher = new EventPublishingRunListener(app, args);
            ApplicationEnvironment env = new ApplicationEnvironment();
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强前");
            for (PropertySource<?> ps : env.getPropertySources()) {
                System.out.println(ps);
            }
            publisher.environmentPrepared(new DefaultBootstrapContext(), env);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后");
            for (PropertySource<?> ps : env.getPropertySources()) {
                System.out.println(ps);
            }
    
        }
    
        private static void test1() {
            SpringApplication app = new SpringApplication();
            ApplicationEnvironment env = new ApplicationEnvironment();
    
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强前");
            for (PropertySource<?> ps : env.getPropertySources()) {
                System.out.println(ps);
            }
            ConfigDataEnvironmentPostProcessor postProcessor1 = new ConfigDataEnvironmentPostProcessor(new DeferredLogs(), new DefaultBootstrapContext());
            postProcessor1.postProcessEnvironment(env, app);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后");
            for (PropertySource<?> ps : env.getPropertySources()) {
                System.out.println(ps);
            }
            RandomValuePropertySourceEnvironmentPostProcessor postProcessor2 = new RandomValuePropertySourceEnvironmentPostProcessor(new DeferredLog());
            postProcessor2.postProcessEnvironment(env, app);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后");
            for (PropertySource<?> ps : env.getPropertySources()) {
                System.out.println(ps);
            }
            System.out.println(env.getProperty("server.port"));
            System.out.println(env.getProperty("random.int"));
            System.out.println(env.getProperty("random.int"));
            System.out.println(env.getProperty("random.int"));
            System.out.println(env.getProperty("random.uuid"));
            System.out.println(env.getProperty("random.uuid"));
            System.out.println(env.getProperty("random.uuid"));
        }
    }

6、绑定 spring.main 到 SpringApplication 对象(*)

把配置文件中的值赋给SpringApplication的默认属性值

public class Step6 {
    // 绑定 spring.main 前缀的 key value 至 SpringApplication, 请通过 debug 查看
    public static void main(String[] args) throws IOException {
        SpringApplication application = new SpringApplication();
        ApplicationEnvironment env = new ApplicationEnvironment();
        env.getPropertySources().addLast(new ResourcePropertySource("step6", new ClassPathResource("step6.properties")));
        System.out.println(application);
        Binder.get(env).bind("spring.main", Bindable.ofInstance(application));
        System.out.println(application);
    }

7、打印 banner(*)

public class Step7 {
    public static void main(String[] args) {
        ApplicationEnvironment env = new ApplicationEnvironment();
        SpringApplicationBannerPrinter printer = new SpringApplicationBannerPrinter(
                new DefaultResourceLoader(),
                new SpringBootBanner()
        );
        // 测试文字 banner
//        env.getPropertySources().addLast(new MapPropertySource("custom", Map.of("spring.banner.location","banner1.txt")));
        // 测试图片 banner
//        env.getPropertySources().addLast(new MapPropertySource("custom", Map.of("spring.banner.image.location","banner2.png")));
        // 版本号的获取
        System.out.println(SpringBootVersion.getVersion());
        printer.print(env, Step7.class, System.out);
    }
}

8、创建容器

    private static GenericApplicationContext createApplicationContext(WebApplicationType type) {
        GenericApplicationContext context = null;
        switch (type) {
            case SERVLET -> context = new AnnotationConfigServletWebServerApplicationContext();
            case REACTIVE -> context = new AnnotationConfigReactiveWebServerApplicationContext();
            case NONE -> context = new AnnotationConfigApplicationContext();
        }
        return context;
    }

9、准备容器发布

  • application context 已初始化事件3️⃣

10、加载 bean 定义

  • 发布 application prepared 事件4️⃣

            DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();
            AnnotatedBeanDefinitionReader reader1 = new AnnotatedBeanDefinitionReader(beanFactory);
            XmlBeanDefinitionReader reader2 = new XmlBeanDefinitionReader(beanFactory);
            ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanFactory);
    
            reader1.register(Config.class);
            reader2.loadBeanDefinitions(new ClassPathResource("b03.xml"));
            scanner.scan("com.itheima.a39.sub");
    

 11、refresh 容器

  • 发布 application started 事件5️⃣  

12、执行 runner

  • 发布 application ready 事件6️⃣

  • 这其中有异常,发布 application failed 事件7️⃣

springboot自动配置原理 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值