• springboot之一:配置文件(内外部配置优先顺序+properties、xml、yaml基础语法+profile动态切换配置、激活方式)


    配置的概念:

    Spring Boot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties或者application.yml(application.yaml)进行配置。

    注意配置文件的命名必须是application开头。

    优先顺序:

    在同一级目录下优先级为:properties > yml > yaml

    内部配置优先顺序:

    1. file:../config/ :当前项目下的/config目录下直接放的配置文件

    2. file:../config/xxx/:当前项目下的/config目录下的子路径下放的配置文件

    3. file:../ :当前项目的根目录中的配置文件

    4. classpath:/config/:classpath的/config目录的配置文件

    5. classpath:/ :classpath的根目录的配置文件

    java和resources的文件会被打包到classes的目录

    properties和yml实际上会被打包到classpath路径下

    优先级顺序如图:

    外部配置优先顺序:

    https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

    properties、xml、yml格式比对:

    properties:

    1. server.port=8080
    2. server.address=127.0.0.1

    xml:

    1. <server>
    2. <port>8080port>
    3. <address>127.0.0.1address>
    4. server>

    yml:

    1. server:
    2. port:8080
    3. address:127.0.0.1

    Yaml(yml):

    • 大小写有区别
    • 数据前必须有空格(空格数目无所谓)作为分隔符,否则不识别该数据。
    • 缩进的空格数目不重要,但是相同层级的元素要左对齐。
    • 注释的话快捷键也是ctrl+/,是#+空格。
    • 参数引用的话用${}包裹起来即可。

    对象(map):键值对的集合

    1. person:
    2. name: zhangsan
    3. # 行内写法
    4. person: {name: zhangsan}

    数组

    1. address:
    2. - beijing
    3. - shanghai
    4. # 行内写法
    5. address: [beijing,shanghai]

     纯量:单个的、不可再分的值。一般都是字符串

    1. msg1: 'hello \n world' # 单引忽略转义字符
    2. msg2: "hello \n world" # 双引识别转义字符

    读取配置内容:

    @Value

    application.yml的对象,用到几个,就要注入几个。

    application.yml中

    1. server:
    2. port: 8082
    3. name: abc
    4. Person:
    5. name: zhangsan
    6. age: 20
    7. Student:
    8. name: ${name} # 占位符,表示abc那个name会传输到这里
    9. address:
    10. - beijing
    11. - shanghai
    12. msg1: 'hello \n world' # 不会识别转义字符
    13. msg2: "hello \n world" # 会识别转义字符

    HelloController中

    1. package com.example.springini.controller;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. public class HelloController {
    7. @Value("${name}")
    8. //和application.yml的键值对的键要同名,与下一行的私有成员名字无关
    9. private String nname;
    10. @Value("${Person.name}")
    11. private String name2;
    12. @Value("${Person.age}")
    13. private int age;
    14. @Value("${Student.name}")
    15. private String name3;
    16. @Value("${address[0]}")
    17. private String addr;
    18. @Value("${msg1}")
    19. private String msg11;
    20. @Value("${msg2}")
    21. private String msg22;
    22. @RequestMapping("/hello2")
    23. public String hello2()
    24. {
    25. System.out.println(nname);
    26. System.out.println(name2);
    27. System.out.println(name3);
    28. System.out.println(age);
    29. return "hello springboot";
    30. }
    31. @RequestMapping("/hello")
    32. public String hello()
    33. {
    34. System.out.println(addr);
    35. System.out.println(msg11);
    36. System.out.println(msg22);
    37. return "hello springboot";
    38. }
    39. }

    @Autowired+Environment

    将application.yml一次性作为一个对象全都注入。

    HelloController中

    1. package com.example.springini.controller;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.core.env.Environment;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. @RestController
    8. public class HelloController_by_environment {
    9. @Autowired
    10. private Environment env;
    11. @RequestMapping("/hello")
    12. public void hello()
    13. {
    14. System.out.println(env.getProperty("name"));
    15. System.out.println(env.getProperty("Person.name"));
    16. System.out.println(env.getProperty("address[0]"));
    17. System.out.println(env.getProperty("msg1"));
    18. System.out.println(env.getProperty("msg2"));
    19. }
    20. }

    @Autowired+ConfigurationProperties

    将配置内容与对象相互绑定。

    新建Person类中

    1. package com.example.springini;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. import org.springframework.stereotype.Component;
    4. @Component //表示这个Person类被识别成Bean
    5. @ConfigurationProperties(prefix = "person")
    6. //如果不指定prefix为person,那么它不一定找到配置文件的person底下的两个字段name和age
    7. //而可能去找到单独的两个字段name和age。
    8. public class Person {
    9. private String name;//命名必须和yml文件中的键值对的键保持一致
    10. private int age;
    11. public String getName() {
    12. return name;
    13. }
    14. public void setName(String name) {
    15. this.name = name;
    16. }
    17. public int getAge() {
    18. return age;
    19. }
    20. public void setAge(int age) {
    21. this.age = age;
    22. }
    23. }

    HelloController中

    1. package com.example.springini.controller;
    2. import com.example.springini.Person;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.core.env.Environment;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. @RestController
    8. public class HelloController_by_ConfigurationProperties {
    9. @Autowired
    10. private Person p;
    11. @RequestMapping("/hello")
    12. public void hello()
    13. {
    14. System.out.println(p.getName()+" : "+p.getAge());
    15. }
    16. }

    profile(动态配置切换):

    我们在开发Spring Boot应用时,通常同一套程序会被安装到不同环境,比如:开发、测试、生产等。其中数据库地址、服务器端口等等配置都不同,如果每次打包时,都要修改配置文件,那么非常麻烦。profile功能就是来进行动态配置切换的。

    配置方式:

    多profile文件方式

    • application-dev.properties/yml  开发环境
    • application-test.properties/yml  测试环境
    • application-pro.properties/yml   生产环境

    yml多文档方式

    在yml中使用----分隔不同配置

    激活方式:

    配置文件

    在配置文件中配置:spring.profiles.active=dev

    虞拟机参数

    在VM options指定:-Dspring.profiles.active=dev

    命令行参数

    java-jar xxx.jar --spring.profiles.active=dev

  • 相关阅读:
    NFTScan | 09.04~09.10 NFT 市场热点汇总
    AR应用的开发流程
    MybatisPlus搭建项目
    基于智能优化算法的交通流模拟器(Matlab代码实现)
    学会这个Python技能,就可以跟excel说再见了
    Maven知识【IDEA使用Maven&依赖管理】第三章
    西安博物院重现1000年前唐三彩,竟然是3D打印!
    Flink理论—容错之状态后端(State Backends)
    采集数据工具推荐,以及采集数据列表详细图解流程
    《码出高效:Java开发手册》笔记之二-面向对象
  • 原文地址:https://blog.csdn.net/zhiaidaidai/article/details/132637805