目录
yml配置文件中使用中文注解
yml配置文件简单语法及小坑
yml配置文件中使用中文注解
问题
我们在yml中写注解一般是这样的 #xxxx
当我们启动时我们会遇到这样的问题
Failed to load property source from
'file:/D:/idea/bonc/server/monitor-streaming/target/classes/application.yml'
(classpath:/application.yml)
根本原因
因为我们在的yml的文件格式时GBK的 我们的中文注释在target文件中是乱码的
解决
修改文件格式 文件格式都改为UTF-8
yml配置文件简单语法及小坑
yml文件使用方法
1-语法
K : (空格)V
表示一对键值对,以空格缩进来控制层级关系,只要左对齐的一列数据,都是一个层级的。属性和值是大小写敏感
2-写法
普通值
字符串默认不加单引号或者双引号;双引号,不会转义字符串里面的特殊字符,特殊字符会作为本身想表示的意思单引号:会转义特殊字符,特殊字符只会是一个普通的字符串数据特殊情况:如 00013 ,类似的数值要加上单引号,否则读取时会出错。
对象,map(属性和值) (键值对)- user:
- userName: "小明"
- boss: true
- birth: 2022/07/13
- age: 20
- ##============行内写法
- user:{userName: "小明",boss: true,birth: 2022/07/13,age: 20}
复制代码 数组(List,Set)
用 - 值 表示数组中的一个元素 - pets:
- - cat
- - dog
- - pig
- #=====行内
- pets: [cat,dog,pig]
复制代码 举个栗子:
pet实体 - package cn.maggie.bussiness.entity;
- import lombok.Data;
- import lombok.ToString;
- @Data
- @ToString
- public class Pet {
- /**
- * 名字
- */
- private String name;
- /**
- * 体重w
- */
- private String weight;
- }
复制代码user实体–读取配置组件 - package cn.maggie.bussiness.entity;
- import lombok.Data;
- import lombok.ToString;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- import java.math.BigDecimal;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- @Component
- @Data
- @ToString
- @ConfigurationProperties(prefix = "user")
- public class User {
- private String userName;
- private Boolean boss;
- private Date birth;
- private Integer age;
- private Pet pet;
- private String [] interests;
- private List<String> animal;
- private Map<String,Object> score;
- private Set<BigDecimal> salary;
- private Map<String ,List<Pet>> allPets;
- }
复制代码yml文件 - user:
- userName: "小明"
- boss: true
- birth: 2022/07/13
- age: 20
- # 数组 2种写法
- # interests: [打球,旅游]
- interests:
- - '喝水'
- - 睡觉
- animal:
- - 阿猫
- - 阿狗
- # map集合 2种写法
- # score: [math: 90,English: 100,chainses: 30]
- sore:
- math: 90
- english: 100
- chainese: 90
- # set集合 2种写法
- # salary: [22.0,333.90]
- salary:
- - 22.90
- - 33.80
- #对象类型 --键值对
- pet:
- name: 小哈
- weight: 9
- # map复杂集合 --2种写法
- allPets:
- sick:
- - {name: 红红,weight: 99}
- - name: niuniu
- weight: 88
- health: [{name: 胖胖,weight: 79},{name: 小白,weight: 90}]
复制代码测试 -启动类打印输出 - package cn.maggie.bussiness;
- import cn.maggie.bussiness.entity.User;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.ConfigurableApplicationContext;
- @SpringBootApplication
- public class BussinessApplication {
- public static void main(String[] args) {
- ConfigurableApplicationContext run = SpringApplication.run(BussinessApplication.class, args);
- User user = run.getBean(User.class);
- System.out.println("============================>"+user.toString());
- }
- }
复制代码 配置文件注入值数据校验
@Validated//JSR303数据校验,此注解加于配置类上
属性可用到的注解
多环境profile- server:
- port: 8080
- spring:
- profiles:
- active: dev #激活,默认就是8080
- ---
- server:
- port: 8083
- spring:
- profiles: test
- ---
- server:
- port: 8081
- spring:
- profiles: prod #指定属于哪个配置
复制代码引入此依赖,自定义bean与配置文件绑定时,配置文件会有提醒 - <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
- <!--打包时排除此包-->
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <excludes>
- <!-- 打包时,排除此包-->
- <exclude>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- </exclude>
- </excludes>
- </configuration>
- </plugin>
- </plugins>
- </build>
复制代码以上为个人经验,希望能给大家一个参考,也希望大家多多支持中国红客联盟。 |