• springboot 核心技术---配置文件


    1、文件类型

    1.1、properties

    同以前的properties用法

    1.2、yaml

    YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写。
    在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。 
    
    非常适合用来做以数据为中心的配置文件
    
    • 1
    • 2
    • 3
    • 4
    1.2.1、基本语法
    ● key: value;kv之间有空格
    ● 大小写敏感
    ● 使用缩进表示层级关系
    ● 缩进不允许使用tab,只允许空格
    ● 缩进的空格数不重要,只要相同层级的元素左对齐即可
    ● '#'表示注释
    ● 字符串无需加引号,如果要加,''与""表示字符串内容 会被 转义/不转义
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1.2.2、数据类型
    • 字面量:单个的、不可再分的值。date、boolean、string、number、null
    k: v
    
    • 1
    • 对象:键值对的集合。map、hash、set、object
    行内写法:  k: {k1:v1,k2:v2,k3:v3}
    #或
    k: 
      k1: v1
      k2: v2
      k3: v3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 数组:一组按次序排列的值。array、list、queue
    行内写法:  k: [v1,v2,v3]
    #或者
    k:
     - v1
     - v2
     - v3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1.2.3、yaml的用法

    bean层下创建两个类

    @Component
    @ConfigurationProperties(prefix = "person")
    @Data
    @ToString
    public class Person {
        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<Double> salarys;
        private Map<String, List<Pet>> allPets;
    }
    
    @Data// set,get方法
    @ToString //toString方法
    public class Pet {
        private String name;
        private Double weight;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    resources目录下创建application.yml

    person:
      userName: zhangsan
      boss: true
      birth: 2019/12/09
      age: 18
      interests: #interests: [篮球,足球]
        - 篮球
        - 足球
      animal: [,]
      #score:
       #english: 80
       #math: 90
      score: {english: 80,math: 90}
      salarys:
        - 99.98
        - 99.99
      pet:
        name: 小白
        weight: 30.33
      allPets:
        sick:
          - {name: 小黑,weight: 88.88}
          - name: 小蓝
            weight: 33.33
          - name: 小绿
            weight: 22.22
        health: [{name: 小白,weight: 66.66},{name: 小银,weight: 77.77}]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    controller层创建HelloController类

    @RestController
    public class HelloController {
    
        @Autowired
        Person person;
    
        @RequestMapping("/person")
        public Person person(){
            return person;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行主程序中的main方法,浏览器访问localhost:8080/person即可

    2、yml自定义配置提示

    配置springboot配置注解处理器
    在pom.xml中配置依赖

    <dependency>
    	<groupId>org.springframework.bootgroupId>
    	<artifactId>spring-boot-configuration-processorartifactId>
    	<optional>trueoptional>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在pom.xml中的插件位置里面设置打包时不加入配置文件绑定的信息

    <configuration>
    	<excludes>
    		<exclude>                            
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-configuration-processorartifactId>
    		exclude>
    	excludes>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    【 java 常用类】日期相关 API 操作
    zblog翻译插件-zblog自动采集翻译插件免费
    【深度学习】Generative Adversarial Network 生成式对抗网络(GAN)
    【数据结构】【程序填空】赫夫曼解码
    数据分析知识点搜集(纯粹的搜集)
    苹果 M1 支持 Linux 最新进展;英特尔发布“GSC”Linux 驱动程序;Linux 基金会研究揭示开源趋势 | 开源日报
    Vuex从了解到实际运用(二),获取vuex中的全局状态(state,getters)
    CTF-Web(3)文件上传漏洞
    华为OD机试真题 Java 实现【AI面板识别】【2023 B卷 100分】,附详细解题思路
    视频怎么压缩?把视频压缩的小一点这样做
  • 原文地址:https://blog.csdn.net/qq_53022114/article/details/127650438