SpringBoot启动执行顺序

1.static、construct、PostConstruct
package com.wlfy.www.teststartupsequence;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author wl
 * @description TestPostConstruct
 * @date 2022/4/8 21:01
 */
@Component
public class TestPostConstruct {
    static {
        System.out.println("static");
    }

    public TestPostConstruct() {
        System.out.println("construct");
    }

    @PostConstruct
    public void init() {
        System.out.println("PostConstruct");
    }

}

2.CommandLineRunner

package com.wlfy.www.teststartupsequence;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author wl
 * @description TestCommandLineRunner
 * @date 2022/4/8 21:07
 */
@Order(2)
@Component
public class TestCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("Order 2 : TestCommandLineRunner");
    }
}

3.ApplicationRunner

package com.wlfy.www.teststartupsequence;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author wl
 * @description TestApplicationRunner
 * @date 2022/4/8 21:06
 */
@Component
@Order(1)
public class TestApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("order 1 : TestApplicationRunner");
    }
}

SpringBoot启动执行顺序:
static
construct
PostConstruct
order 1 : TestApplicationRunner
Order 2 : TestCommandLineRunner

总结
Spring应用启动过程中,肯定是要自动扫描有@Component注解的类,加载类并初始化对象进行自动注入。加载类时首先要执行static静态代码块中的代码,之后再初始化对象时会执行构造方法。最新 Spring Boot 面试题整理好了,点击Java面试库小程序在线刷题。

在对象注入完成后,调用带有@PostConstruct注解的方法。当容器启动成功后,再根据@Order注解的顺序调用CommandLineRunner和ApplicationRunner接口类中的run方法。

因此,加载顺序为static>constructer>@PostConstruct>CommandLineRunner和ApplicationRunner.

<think>好的,我现在需要回答用户关于Spring Boot启动时配置文件执行顺序的问题。首先,我得回顾一下Spring Boot处理配置文件的机制,确保自己理解正确。 用户可能对Spring Boot的配置文件加载顺序不太清楚,特别是不同配置源的优先级。我记得Spring Boot支持多种配置方式,比如application.properties、application.yml、环境变量、命令行参数等,这些配置源有不同的优先级,优先级高的会覆盖低的。 首先,我需要确认各个配置文件的加载顺序。根据官方文档,配置文件的加载顺序应该是从jar包内部的application.properties到外部的,再到特定的profile配置,比如application-dev.properties。另外,命令行参数和环境变量的优先级通常更高。 不过可能有些细节需要注意,例如,带profile的配置文件会覆盖不带profile的。还有,不同位置的配置文件,比如classpath下的、当前目录下的、config目录下的,它们的优先级也不同。比如,当前目录下的config文件夹里的配置文件优先级高于classpath里的。 然后,我需要考虑如何组织这些信息,让用户能够清晰理解。可能需要按优先级从高到低列出各个配置源,并给出例子。例如,命令行参数优先级最高,其次是环境变量,然后是外部的配置文件,最后是内部的。 同时,要避免错误,比如不要混淆properties和yml文件的顺序,它们的优先级应该是相同的,只是文件格式不同。而带profile的配置会优先于默认的application文件。 可能还需要提到spring.config.location这个属性,用户可以通过它自定义配置文件的位置,这会改变默认的加载顺序。不过这种情况下,用户指定的位置会有更高的优先级。 最后,总结一下整体的加载顺序,确保覆盖所有常见的配置源,并强调覆盖规则,即后加载的配置会覆盖之前的,但高优先级的配置源会先加载,所以高优先级的配置会生效。</think>Spring Boot启动时配置文件的执行顺序遵循特定的优先级规则,不同位置的配置文件会被加载并覆盖,优先级高的配置会生效。以下是关键顺序说明(从高到低): --- ### **1. 命令行参数** 通过`--key=value`传递的参数优先级最高。 示例:`java -jar app.jar --server.port=8081` --- ### **2. 环境变量** 操作系统或容器设置的环境变量。 示例:`export SERVER_PORT=8082` --- ### **3. 外部配置文件(优先级由高到低)** - **`./config/`目录下的文件** 项目根目录下的`config`子目录。 路径示例:`项目根目录/config/application.yml` - **当前目录下的配置文件** 直接放在项目根目录下。 路径示例:`项目根目录/application.yml` - **`classpath:/config/`目录下的文件** 类路径下的`config`子目录。 路径示例:`src/main/resources/config/application.properties` - **`classpath:`根目录下的文件** 类路径根目录下的默认配置文件。 路径示例:`src/main/resources/application.yml` --- ### **4. Profile专属配置** 激活的Profile(如`dev`)对应的配置文件会覆盖默认配置。 文件名格式:`application-{profile}.properties`,例如: - `application-dev.yml`(当`spring.profiles.active=dev`时生效) --- ### **5. @PropertySource注解** 通过`@PropertySource`注解手动指定的配置文件,优先级低于默认配置文件。 --- ### **覆盖规则总结** 1. **同类型配置**:后加载的覆盖先加载的。 2. **不同类型配置**:优先级高的覆盖优先级低的。 3. **Profile配置**:优先于非Profile配置。 --- ### **验证配置顺序** 通过启动日志中的`PropertySources`部分可查看最终生效的配置顺序: ```log 2023-10-01 INFO o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:./config/application.yml' 2023-10-01 INFO o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:./application.yml' 2023-10-01 INFO o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'classpath:/config/application.yml' 2023-10-01 INFO o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'classpath:/application.yml' ``` --- ### **自定义配置位置** 通过`spring.config.location`指定额外配置路径(优先级高于默认路径): ```bash java -jar app.jar --spring.config.location=classpath:/custom-config/,file:/etc/app/ ``` 掌握这些规则,可以灵活管理不同环境下的配置覆盖关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值