[JAVA] 如何在yml配置文件中使用中文注解

2101 0
Honkers 2022-11-9 09:19:46 | 显示全部楼层 |阅读模式
目录

    yml配置文件中使用中文注解
      问题根本原因解决
    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(属性和值) (键值对)
  1. user:
  2.   userName: "小明"
  3.   boss: true
  4.   birth: 2022/07/13
  5.   age: 20
  6. ##============行内写法
  7. user:{userName: "小明",boss: true,birth: 2022/07/13,age: 20}
复制代码
数组(List,Set)
用 - 值 表示数组中的一个元素
  1. pets:
  2. - cat
  3. - dog
  4. - pig
  5. #=====行内
  6. pets: [cat,dog,pig]
复制代码
举个栗子:
pet实体
  1. package cn.maggie.bussiness.entity;
  2. import lombok.Data;
  3. import lombok.ToString;
  4. @Data
  5. @ToString
  6. public class Pet {
  7.   /**
  8.   * 名字
  9.   */
  10.   private String name;
  11.   /**
  12.   * 体重w
  13.   */
  14.   private String weight;
  15. }
复制代码
user实体–读取配置组件
  1. package cn.maggie.bussiness.entity;
  2. import lombok.Data;
  3. import lombok.ToString;
  4. import org.springframework.boot.context.properties.ConfigurationProperties;
  5. import org.springframework.stereotype.Component;
  6. import java.math.BigDecimal;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;
  11. @Component
  12. @Data
  13. @ToString
  14. @ConfigurationProperties(prefix = "user")
  15. public class User {
  16.   private String userName;
  17.   private Boolean boss;
  18.   private Date birth;
  19.   private Integer age;
  20.   private Pet pet;
  21.   private String [] interests;
  22.   private List<String> animal;
  23.   private Map<String,Object> score;
  24.   private Set<BigDecimal> salary;
  25.   private Map<String ,List<Pet>> allPets;
  26. }
复制代码
yml文件
  1. user:
  2. userName: "小明"
  3. boss: true
  4. birth: 2022/07/13
  5. age: 20
  6. # 数组 2种写法
  7. # interests: [打球,旅游]
  8. interests:
  9.   - '喝水'
  10.   - 睡觉
  11. animal:
  12.   - 阿猫
  13.   - 阿狗
  14. # map集合 2种写法
  15. # score: [math: 90,English: 100,chainses: 30]
  16. sore:
  17.   math: 90
  18.   english: 100
  19.   chainese: 90
  20. # set集合 2种写法
  21. # salary: [22.0,333.90]
  22. salary:
  23.   - 22.90
  24.   - 33.80
  25. #对象类型 --键值对
  26. pet:
  27.   name: 小哈
  28.   weight: 9
  29. # map复杂集合 --2种写法
  30. allPets:
  31.   sick:
  32.    - {name: 红红,weight: 99}
  33.    - name: niuniu
  34.     weight: 88
  35.   health: [{name: 胖胖,weight: 79},{name: 小白,weight: 90}]
复制代码
测试 -启动类打印输出
  1. package cn.maggie.bussiness;
  2. import cn.maggie.bussiness.entity.User;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.ConfigurableApplicationContext;
  6. @SpringBootApplication
  7. public class BussinessApplication {
  8.   public static void main(String[] args) {
  9.     ConfigurableApplicationContext run = SpringApplication.run(BussinessApplication.class, args);
  10.     User user = run.getBean(User.class);
  11.     System.out.println("============================>"+user.toString());
  12.   }
  13. }
复制代码
配置文件注入值数据校验
@Validated//JSR303数据校验,此注解加于配置类上
属性可用到的注解


多环境profile
  1. server:
  2.   port: 8080
  3. spring:
  4.   profiles:
  5.     active: dev #激活,默认就是8080
  6. ---
  7. server:
  8.   port: 8083
  9. spring:
  10.   profiles: test
  11. ---
  12. server:
  13.   port: 8081
  14. spring:
  15.   profiles: prod #指定属于哪个配置
复制代码
引入此依赖,自定义bean与配置文件绑定时,配置文件会有提醒
  1. <dependency>
  2.       <groupId>org.springframework.boot</groupId>
  3.       <artifactId>spring-boot-configuration-processor</artifactId>
  4.       <optional>true</optional>
  5. </dependency>
  6. <!--打包时排除此包-->
  7. <build>
  8.         <plugins>
  9.             <plugin>
  10.                 <groupId>org.springframework.boot</groupId>
  11.                 <artifactId>spring-boot-maven-plugin</artifactId>
  12.                 <configuration>
  13.                     <excludes>
  14. <!--                        打包时,排除此包-->
  15.                         <exclude>
  16.                             <groupId>org.springframework.boot</groupId>
  17.                             <artifactId>spring-boot-configuration-processor</artifactId>
  18.                         </exclude>
  19.                     </excludes>
  20.                 </configuration>
  21.             </plugin>
  22.         </plugins>
  23.     </build>
复制代码
以上为个人经验,希望能给大家一个参考,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4004
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行