SpringBoot配置文件

基本语法

  • key: value形式,kv之间有空格
  • 大小写敏感
  • 使用缩进表示层级关系,缩进不允许使用tab,只允许空格。缩进的空格数不重要,只要相同层级的元素左对齐即可
  • '#'表示注释
  • 字符串无需加引号,如果要加,也可

数据类型

  • 基本:date、boolean、string、number、null
  • 对象:map、hash、set、object,键值对的集合

写法举例

行内写法

对象:

 k: {k1:v1,k2:v2,k3:v3}

数组:

 k: [v1,v2,v3]

级联写法

对象:

spring:
  cloud:
    nacos:
      discovery:
        username: nacos
        password: nacos
        server-addr: 192.168.1.61:8848
        namespace: cml
      config:
        server-addr: 192.168.1.61:8848
        file-extension: yaml
        namespace: public

数组:

cml:
  - 1
  - 2
  - 3

配置提示

    <!-- configuration-processor  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>


用法举例

逗号分隔

配置:

test: 
    list: aaa,bbb,ccc

读取配置:

@Value("#{'${test.list:}'.split(',')}")  

默认值

单个值

配置:

cml:
  age: 20

读取配置:

@Value("${cml.age:18}")
private String age;


//如果没有 ngh.cameraIndexCodes 这个配置,或者这个配置为空,都将赋值默认值 ‘空字符串’ 

@Value("${ngh.cameraIndexCodes:}")
private String manoeuvreTip;


集合值

配置:


autoLog:   
  ignoreMethods: 

读取配置:


@Value("${autoLog.ignoreMethods:\"login\", \"changePassword\", \"modifyPassword\", 
\"uploadFile\", \"downloadLatestFileByType\", \"syncPurchaseRequirement\"}")
private List<String> ignoreMethods;


映射到类

配置:


cml:
  admin:
    defaultUserName: admin
    defaultPassword: 111111
    defaultUserName1: admin1
    defaultPassword1: 222222

读取配置:

  • ConfigurationProperties注解将加载配置项内容到类属性上面
  • Component:将类注册到 Spring 容器,这样其他类可以直接 Autowired 使用
  • RefreshScope :配置更改,自动刷新,不用重启启动服务

@RefreshScope
@Component
@ConfigurationProperties(prefix = "cml.admin")
@Data
@ToString
public class Account  implements Serializable {
    private  String defaultUserName;
    private  String defaultPassword;
    private  String defaultUserName1;
    private  String defaultPassword1;
}


注意事项

  • @Value 不可和@AllArgsConstructor 同时使用

引用

https://www.cnblogs.com/coderacademy/p/18181462

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ramble_Naylor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值