springboot 配置文件加载顺序,配置文件格式
1.关闭横幅或者加载自己的横幅。注意命名:banner文件,注意文件命名只能是 banner.txt 或 Banner.txt
public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(Springboot01Application.class); //关闭横幅 //springApplication.setBannerMode(Banner.Mode.OFF); springApplication.run(args); }

2.默认配置文件。允许有三种格式:.yml .yaml .properties

三种文件同时存在则互为补充,但加载顺序每个版本并不统一,这里用的3.1.2版本,三个文件同时配置 server.port 属性进行验证,结果优先级由高到底 .properties > .yml > .yaml
尽管 .properties 优先级最高!但在开发时,由于可读性更强,使用 .yml 格式多一些 。
yml基本语法:
- 
- k:(空格)v:表示一对键值对(空格必须有);
 - 以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
 - 属性和值也是大小写敏感;
 - 如果有特殊字符% & 记得用单引号(‘)包起来
 
 
3.外部约定配置文件加载顺序:优先级 5>4>3>2>1
- classpath根目录下的
 - classpath根config/
 - 项目根目录
 - 项目根目录/config
 - 直接子目录/config
 

java -jar .\springboot01-0.0.1-SNAPSHOT.jar --spring.config.location=F:\config/ 注意以 ‘/’ 结尾 优先级最高
默认会寻找同级目录下的配置

springboot01是a_parent的子模块存在的。3.4两种情况不常用

官网:
- optional: classpath:/
 - optional: classpath:/config/
 - optional: file:./
 - optional: file:./config/*/
 - optional: file:./config/
 - optional: classpath:custom-config/ --spring.config.location
 - optional: file:./custom-config/ --spring.config.location
 
4.Profile文件的加载

- 在配置文件中指定 spring.profiles.active=dev 则启动端口为 8081 (互补)
 - 命令行:java -jar .\springboot01-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro 启动端口为8082 (互补)
 - 还可以通过spring.config.name来改变默认的配置文件 java -jar .\springboot01-0.0.1-SNAPSHOT.jar --spring.config.name=application-dev 启动端口为8081(不互补)
 - 通过 --spring.config.location来直接指定配置文件 java -jar .\springboot01-0.0.1-SNAPSHOT.jar --spring.config.location=F:\config\application.yml 启动端口为8080(不互补)
 
5.配置文件读取方式 低---高
- 
@PropertySource @Configuration类上的注释。请注意,Environment在刷新应用程序上下文之前,不会将此类属性源添加到中。现在配置某些属性(如logging.*和spring.main.*在刷新开始之前先读取)为时已晚。
 - 会和约定的配置文件形成互补
 - 一定要指定.properties配置
 
        @PropertySource("classpath:appSource.properties")
- 
默认属性(通过设置指定SpringApplication.setDefaultProperties)。
 - 会和约定的配置文件形成互补
 
public static void main(String[] args) throws IOException { SpringApplication springApplication = new SpringApplication(Springboot01Application.class); // 创建Properties Properties properties = new Properties(); // 通过当前类的ClassLoader InputStream is= Springboot01Application.class.getClassLoader() .getResourceAsStream("app.properties"); // 将输入流读取成properties properties.load(is); springApplication.setDefaultProperties(properties); springApplication.run(args); } }
- 
配置数据(例如application.properties文件)
 - 约定配置文件
 - 
操作系统环境变量。
 - 会使约定配置文件失效
 
idea 配置:Environment variables: spring.config.location=D:\config/ (换成你自己的路径)
- windows 通过set 属性设置window环境变量 ‘set spring.config.location=’ (等号后面不赋值就是清空环境变量)
 
        set spring.config.location=D:\config/
         java -jar 03_extern_configuration-0.0.1-SNAPSHOT.jar
- 
Java系统属性(System.getProperties())。
 - 会使约定配置文件失效
 - idea: VM options: -Dspring.config.location=D:\config\application-java.properties ()
 
- 命令行java属性
 
        java -Dspring.config.location=D:\config\application-java.properti es -jar 03_extern_configuration-0.0.1-SNAPSHOT.jar
- 
的JNDI属性java:comp/env。
 - 
ServletContext 初始化参数。
 
    ServletContext 的配置标签需要写到 web-app (根标签)中 ,具体如下: 
<context-param> <param-name>spring.config.location</param-name> <param-value>xxx.properties</param-value> </context-param>
- 
ServletConfig 初始化参数。
 
    ServletConfig 的配置标签需要写到 Servlet 标签中,标签如下: 
<init-param> <param-name>spring.config.location</param-name> <param-value>xxx.properties</param-value> </init-param>
- 
来自的属性SPRING_APPLICATION_JSON(嵌入在环境变量或系统属性中的嵌入式JSON)。
 - 
命令行参数。会使约定配置文件失效
 
     java -jar configuration_file-0.0.1-SNAPSHOT.jar --spring.config.location=D:/application.properties
- 
properties测试中的属性。可用于测试应用程序的特定部分@SpringBootTest的测试注释和注释。
 - 
@TestPropertySource 测试中的注释。
 
用在单元测试上的@TestPropertySource("classpath:appSource.properties")

- 
$HOME/.config/spring-boot当devtools处于活动状态时,目录中的Devtools全局设置属性。
 
6.配置文件值注入
我们可以导入配置文件处理器,以后编写配置就有提示了。idea下还需要勾选 ‘启用注解处理’ 选项  
只有标注 @ConfigurationProperties 的bean才会有提示
<!--会生成 META-INF 元数据,用于提示idea自动提示配置文件的--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <!--依赖不会传播--> <optional>true</optional> </dependency>

1. 使用 @Value 单一属性注入:

2.使用 @ConfigurationProperties(prefix = "user") 指定配置前缀 进行一次性注入

松散绑定
user:
  USERNAME: 徐庶
user:
  userName: 徐庶
user:
  user_name: 徐庶
user:
  user-name: 徐庶
以上4种命名是可以自动绑定bean属性 User.username
配置文件各种类型的写法:

3.启动校验:
<!--jsr303 数据校验--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>

4.独立配置数据,使用 @PropertySource 指定独立配置路径 只能使用 .properties 格式的文件


                    
                
                
            
        
浙公网安备 33010602011771号