zoukankan      html  css  js  c++  java
  • SpringBoot配置文件分析

    文件类型

    分析源码能够知道

    配置文件类型有yml、yaml、properties三种类型

    YAML

    SpringBoot推荐使用yaml文件,但是三者都可以;

    yaml是强调以数据做为中心

    经历过的配置:

    xml:标记语言,格式繁琐,麻烦!

    <service>
        <port>8081</port>
    </service>
    

    propeties:kry=value,太过单一,功能有限;

    service.port=8081
    

    yaml:以数据作为中心的配置文件;可以配置:普通的键值对,对象、map、数组、LIst

    service:
        port: 8081
    

    基本格式

    空格严格要求,缩进严格要求!

    基本语法

    普通字段:

    k: v
    name: zhangsan
    

    对象,Map

    k:
        v1:
        v2:
    
    person:
        name: lisi
        age: 3
    
    # 支持行内写法
    person: {name: lisi,age: 3}
    

    数组,List

    k:
        - v1
        - v2
        - v3
      
    pets:
        - cat
        - dog
        - pig
    
    # 支持行内写法
    pets: {cat,dog,pig}
    

    yaml测试实例

    编写实体类

    package com.star.pojo;
    
    import lombok.Data;
    import org.springframework.stereotype.Component;
    
    @Data
    @Component
    public class Dog {
        private String name;
        private Integer age;
    }
    
    package com.star.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    /*
    * SpringBoot配置类底层全部都是@ConfigurationPropeties
    * 可以通过prefix前缀绑定配置文件中的yaml对象
    * 这个类的所有属性,和配置类中的字段进行自动的一一对应
    *
    * */
    @Data
    @Component
    @ConfigurationProperties(prefix = "person")
    public class Person {
        private String name;
        private Integer age;
        private Boolean happy;
        private Date birth;
        private Map<String,Object> maps;
        private List<Object> lists;
        private Dog dog;
    }
    

    配置文件

    person:
      name: zhangsan
      age: 1
      happy: false
      birth: 2020/02/28
      maps: {k1: v1,k2: v2}
      lists:
        - code
        - music
        - game
      dog:
        name: 旺财
        age: 3
    

    测试类

    package com.star;
    
    import com.star.pojo.Person;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class SpringbootApplicationTests {
    
        // 自动导入数据源
        @Autowired
        private Person person;
    
        @Test
        void contextLoads() {
            System.out.println(person);
        }
    
    }
    

    测试结果;

    JSR303校验

    JSR303是Java为Bean数据合法性校验所提供的一个标准规范,叫做 Bean Validation。用于对 Java Bean中的字段值进行验证。

    Spring Boot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理;

    比如我们用邮箱来测试一下

    @Component
    @ConfigurationProperties(prefix = "person")
    @Validated//数据校验
    public class Person {
        @Email(message = "不符合邮箱格式!")//邮箱格式,message:报错信息
        private String email;
    }
    

    配置文件

    person:
        email: 123456
    

    测试类

        // 自动导入数据源
        @Autowired
        private Person person;
    
        @Test
        void contextLoads() {
            System.out.println(person);
        }
    

    测试结果:

    JSR303校验数据的格式有很多,这里只是举一个简单的例子!

    可以参考源码:

    也可以参考下图:

    分析配置文件

    我们可以在yml文件中编写这样的代码来配置服务器的端口号

    server:
      port: 8081
    

    启动项目来测试一下

    为什么写这样的代码会生效呢?

    接下来我们分析一下

    点进自动配置类的随便一个配置类(比如HttpEncodingAutoConfiguration)

    看到前缀为spring.http

    我们在yml中就可以编写

    spring:
      http:
    

    在源码中都能找到对应的属性(比如:httpProperties类中的属性:encoding.cahrset....)

    得出结论:

    • 配置文件中能配置的所有配置,都有对应的配置类
    • 所有的AutoConfig自动配置类,都有对应的XXXProperties类
    • 所有的XXXProperties配置类 都绑定了配置文件的固定的前缀

    总之:能够配置的一切的一切都是从源码中编写;

    多环境配置文件以及配置文件位置

    SpringBoot中的配置文件application.yaml可以放在多个位置:(参考官网)

    file:项目路径

    classpath:resources

    • file:./config

    • file:./

    • classpath:./

    • classpath:/

    默认配置文件的优先级:从上往下

    优先级最低的,别的配置文件可以覆盖!

    有多个配置环境,选定某一个配置文件;

    默认是选择application.yaml

    但是在application.yaml中可以选则特定的配置文件:

    选择application-dev.yaml文件

    spring:
      profiles:
        active: dev
    

    选择application-test.yaml文件

    spring:
      profiles:
        active: test
    

    也可以在一个文件中有多个配置,选择某一个配置

    # 默认
    server:
      port: 8081
    spring:
      profiles:
        active: dev/test
    ---
    server:
      port: 8082
    spring:
      profiles: dev
    ---
    server:
      port: 8083
    spring:
      profiles: test
    

    分割线相当于多个配置文件

  • 相关阅读:
    数据中心网络架构的问题与演进 — NFV
    数据中心网络架构的问题与演进 — 云网融合与 SD-WAN
    数据中心网络架构的问题与演进 — 混合云与 VPC 专有网络
    数据中心网络架构的问题与演进 — SDN
    数据中心网络架构的问题与演进 — Overlay 网络
    ASP.NET MVC- Controllers and Routing- Controller Overview
    ASP.NET MVC- VIEW Creating Page Layouts with View Master Pages Part 4
    ASP.NET MVC- VIEW Using the TagBuilder Class to Build HTML Helpers Part 3
    ASP.NET MVC- VIEW Creating Custom HTML Helpers Part 2
    ASP.NET MVC- VIEW Overview Part 1
  • 原文地址:https://www.cnblogs.com/lmx-181028/p/12411104.html
Copyright © 2011-2022 走看看