Loading

SpringBoot配置文件

默认配置文件

SpringBoot项目在启动时会将 resources 目录下的 application.propertiesapllication.yaml 作为其默认配置文件,可以在该配置文件中对项目进行相关的配置。

例如修改SpringBoot项目启动的默认端口号,在 resources 目录下新建 apllication.yaml文件

server:
  port: 8081
  • 将端口号改为8081

配置文件的加载顺序

如果resources目录下既存在 application.properties 也存在 apllication.yaml,位于相同位置的 application.properties 的优先级高于 application.yaml

对于不同目录下的配置文件,按照如下顺序加载:

  1. 当前项目路径下的config文件夹
  2. 当前项目的根路径
  3. classpath路径下config文件夹
  4. classpath路径下

外部配置加载顺序

外部配置按照按以下顺序(较低的值可以覆盖较早值):

1.默认参数(由SpringApplication.setDefaultProperties 指定)。

2.@Configuration配置文件上 @PropertySource注解加载的参数。

3.配置数据(如application.properties文件)。

4.RandomValuePropertySource 随机数,仅匹配:ramdom.*

5.操作系统环境变量。

6.Java 系统属性 ( System.getProperties())。

7.JNDI 属性来自java:comp/env

8.ServletContext 初始化参数。

9.ServletConfig 初始化参数。

10.来自SPRING_APPLICATION_JSON指定的参数, 如 java -Dspring.application.json='{"name":"xxx"}' -jar springboot.jar

11.命令行参数。

12.单元测试上的 @SpringBootTest 注解指定的参数。

13.单元测试上的TestPropertySource 注解指定的参数。

14.开发者工具 Devtools 全局配置参数。

读取配置文件的内容

SpringBoot 提供了以下 2 种方式读取配置的内容:

  • 使用 @ConfigurationProperties 注解
  • 使用 @Value 注解

application.yaml如下:

server:
  port: 8081

name: tom
age: 22
#对象
person:
  name: tom2   # ${name}
  age: 20
  maps: {k1: v1, k2: v2}
  lists:
    - game
    - code
    - movie

#对象行内写法
person2: {name: jerry, age: 18}

#数组
address:
  - shanghai
  - beijing

#数组行内写法
address2: [hangzhou, nanjing]

msg1: 'hello \n world' #原样输出
msg2: "hello \n world" #会识别转义字符

@Value

使用@Value注解获取配置文件中的值

@RestController
public class HelloController {
    @Value("${name}")
    private String name;

    @Value("${person.name}")
    private String name2;

    @Value("${person.age}")
    private Integer age;

    @Value("${address[0]}")
    private String address1;

    @Value("${msg1}")
    private String msg1;

    @Value("${msg2}")
    private String msg2;

    @RequestMapping("/hello")
    public String hello() {
        System.out.println(name); //tom
        System.out.println(name2); //tom2
        System.out.println(age); //20
        System.out.println(address1); //shanghai
        System.out.println(msg1); //hello \n world
        System.out.println(msg2);
        return "Hello SpringBoot";
    }
}

@ConfigurationProperties

@ConfigurationProperties 注解可以将全局配置文件中的配置数据绑定到 JavaBean 中。

新建User类:

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;

    private Integer age;

    private Map<String,Object> maps;

    private List<Object> lists;

    public Person(String name, Integer age, Map<String, Object> maps, List<Object> lists) {
        this.name = name;
        this.age = age;
        this.maps = maps;
        this.lists = lists;
    }

    public Person() {
    }

    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;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", maps=" + maps +
                ", lists=" + lists +
                '}';
    }
}
  • @ConfigurationProperties 标注在类上,用于批量绑定配置文件中的配置。
  • @ConfigurationPropertie 支持所有类型数据的封装,例如 Map、List、Set、以及对象等。

在controller中测试:

@RestController
public class HelloController {
    @Autowired
    Person person;

    @RequestMapping("/hello")
    public String hello() {
        System.out.println(person); //Person{name='tom2', age=20, maps={k1=v1, k2=v2}, lists=[game, code, movie]}
        return "Hello SpringBoot";
    }
}

profile

在开发SpringBoot应用时,可能会存在不同的环境,比如:开发、测试、生产等,不同的环境配置不同。

一般多profile文件的格式为:

application-{profile}.properties/yaml
  • profile为环境的名称,如dev、test、prod等。

profile的配置可以通过多个profile文件的方式和yaml多文档块的方式。

多profile文件方式

application.properties主配置文件,指定要激活的环境

spring.profiles.active=dev

application-dev.properties

server.port=8081

application-test.properties

server.port=8082

application-prod.properties

server.port=8083

yaml多文档块方式

#切换配置
spring:
  profiles:
    active: prod # 指定激活的文档

---
server:
  port: 8085
#给文档命名为dev
spring:
  config:
    activate:
      on-profile: dev
---
server:
  port: 8086
#给文档命名为test
spring:
  config:
    activate:
      on-profile: test
---
server:
  port: 8087
#给文档命名为prod
spring:
  config:
    activate:
      on-profile: prod

参考文档:https://docs.spring.io/spring-boot/docs/2.4.10/reference/html/spring-boot-features.html#boot-features-spring-application

posted @ 2021-09-18 11:07  charlatte  阅读(219)  评论(0编辑  收藏  举报