• springboot配置文件总结


    录:

    1、springboot配置文件简介
    2、yaml 语法
        2.1、字面量
        2.2、对象、map
        2.3、list 或 set
    3、yml配置文件属性的获取(配合@ConfigurationProperties使用)
    4、@ConfigurationProperties与@Value区别
    5、@PropertySource、@ImportResource
    6、配置文件占位符
    7、Profile多环境支持
    8、配置文件的加载位置
    9、外部配置加载顺序

    1、springboot配置文件简介    <--返回目录

      springboot使用一个全局的配置文件application.properties或application.yml。配置文件放在src/main/resources目录或类路径/config下。

      yml是YAML(YAML Ain't Markup Language)语言的文件,以数据位中心,比json、xml更适合做配置文件。

      全局配置文件可以对一些默认配置进行修改,比如修改项目端口 server.port=8081。

    2、yaml 语法    <--返回目录

      yaml 支持三种数据结构

        - 对象:键值对的集合

        - 数组:一组按次序排列的值

        - 字面量:单个的、不可再分的值

      yaml 基本语法

        - 使用缩进表示层级关系

        - 缩进时不允许使用tab键,只允许使用空格

        - 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

        - 大小写敏感

    2.1、字面量    <--返回目录

        K: V :字面直接写。字符串默认不用加单引号或双引号。

          "": 双引号不会转义字符串里面的特殊字符;特殊字符或作为本身想表示的意思

            name: "zs lisi" => 输出 zs 换行 lisi

          '': 单引号会转义特殊字符,特殊字符最终只是一个普通的字符串数据

            name: 'zs lisi' => 输出 zs lisi

    2.2、对象、map    <--返回目录

    friends: 
        lastName: zs
        age: 10

      或行内写法

    friends: {lastName: zs, age: 10}

    2.3、list 或 set    <--返回目录

    pets:
        - cat
        - dog

      或行内写法

    pets: [cat, dog]

    3、yml配置文件属性的获取(配合@ConfigurationProperties使用)    <--返回目录

      依赖(springboot版本2.1.1.RELEASE)

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

      bean 类

    @Component
    @ConfigurationProperties(prefix="person")
    public class Person {
        private String lastName;
        private Integer age;
        private Boolean boss;
        private Date date;
        
        private Map<String, Object> map;
        private List<String> list;
        private Dog dog;
        
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Boolean getBoss() {
            return boss;
        }
        public void setBoss(Boolean boss) {
            this.boss = boss;
        }
        public Date getDate() {
            return date;
        }
        public void setDate(Date date) {
            this.date = date;
        }
        public Map<String, Object> getMap() {
            return map;
        }
        public void setMap(Map<String, Object> map) {
            this.map = map;
        }
        public List<String> getList() {
            return list;
        }
        public void setList(List<String> list) {
            this.list = list;
        }
        public Dog getDog() {
            return dog;
        }
        public void setDog(Dog dog) {
            this.dog = dog;
        }
        @Override
        public String toString() {
            return "Person [lastName=" + lastName + ", age=" + age + ", boss=" + boss + ", date=" + date + ", map=" + map
                    + ", list=" + list + ", dog=" + dog + "]";
        }
    }

      yml配置

    person: 
      last-name: 张三
      age: 10
      date: 2020/11/19
      boss: false
      map: {k1: v1, k2: v2}
      list: [aaa, bbb]
      dog: 
        name: 小黄
        age: 10
        

      测试类

    package com.oy;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.oy.bean.Person;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestPerson {
        
        @Autowired
        private Person person;
        
        /**
         * Person [lastName=张三, age=10, boss=false, date=Thu Nov 19 00:00:00 CST 2020, 
         * map={k1=v1, k2=v2}, list=[aaa, bbb], dog=Dog [name=小黄, age=10]]
         */
        @Test
        public void printPerson() {
            System.out.println(person);
        }
    
    }

    4、@ConfigurationProperties与@Value区别    <--返回目录

     松散语法:person.lastName  或 person.last-name  或 person.last_name

    5、@PropertySource、@ImportResource    <--返回目录

      @PropertySource:导入properties资源文件

      java bean

    @Validated
    @PropertySource("classpath:person.properties") @Component @ConfigurationProperties(prefix="person")
    public class Person { private String lastName;
       @NotNull private Integer age;
    // getXxx和setXxx省略
    }

      person.properties

    person.lastName=zs

      测试代码

    package com.oy;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.oy.bean.Person;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestPerson {
        
        @Autowired
        private Person person;
        
        @Test
        public void printPerson() {
            System.out.println(person);
        }
    
    }

      @ImportResource:导入spring的配置文件,让配置文件里面的内容生效。

    @ImportResource(locations = {"classpath:beans.xml"})
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
        
    }

    6、配置文件占位符    <--返回目录

      配置文件使用占位符

    person.lastName=${random.uuid}
    person.age=${random.int}
    person.dog.name=${person.hello:hello}_dog

      ${person.hello:hello}_dog: 首先去取person.hello这个属性,找不到取冒号“:”后面的默认值。

      person.lastName=${random.uuid}:随机数 ${random.int}, ${random.long}, ${random.int(10)}, ${random.int[1024,65535]}等

    7、Profile多环境支持    <--返回目录

     激活方式:

      - 命令行: java -jar xxx.jar --spring.profiles.active=dev

      - 配置文件(application.properties): spring.profiles.active=dev

      - jvm参数: -Dspring.profiles.active=dev

    8、配置文件的加载位置    <--返回目录

       springboot启动会扫描一下位置的application.properties或者application.yml文件作为springboot的默认配置文件

    项目根目录/config
    项目根目录/
    classpath:/config
    classpath:/

      优先级由高到低,高优先级的配置会覆盖低优先级的配置。springboot会从这四个位置全部加载主配置文件,形成"互补配置"。

      注意:springboot的那个打包插件默认不会将项目目录下的其他文件打进jar包。

    9、外部配置加载顺序    <--返回目录

      优先级由高到低,形成"互补配置"。

      命令行参数配置例子:java -jar demo.jar --server.port=8081  --spring.profiles.active=dev。

      对上述6-9的总结:由jar包外向jar包内进行寻找;优先加载带profile的。

      如果需要配置的命令行参数过多,可以考虑在jar包外(与jar同一级目录)添加配置文件,比如

    ---

  • 相关阅读:
    django模板导入外部js和css等文件
    django 快速搭建blog
    JS定时器的使用--延时提示框
    JS定时器的使用--数码时钟
    JS定时器的使用--无缝滚动
    初探JavaScript魅力(五)
    初探JavaScript魅力(四)
    初探JavaScript魅力(三)
    初探JavaScript魅力(二)
    PHP正则表达式
  • 原文地址:https://www.cnblogs.com/xy-ouyang/p/14008671.html
Copyright © 2020-2023  润新知