SpringBoot开发笔记-(3)-属性文件读取1-@ConfigurationProperties读取yml配置文件

  1. 3.1 ConfigurationProperties使用方式
    1. 3.1.1. 步骤
    2. 3.1.2. 样例
  2. 3.2 @ConfigurationProperties 和 @Value比较
    1. 3.2.1 @ConfiguratonProperties
    2. 3.2.2 @Value

3.1 ConfigurationProperties使用方式

3.1.1. 步骤

@Component+@ConfigurationProperties(prefix="person")

person是在yml中配置的前缀: person: …

3.1.2. 样例

(1). maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.niewj</groupId>
    <artifactId>springboot-study</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spring-boot-version>2.3.2.RELEASE</spring-boot-version>
    </properties>
    <modules>
        <module>springboot-01-helloworld</module>
    </modules>

    <!-- 表示是一个pom总的父工程 -->
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <!-- springboot测试用例需要的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <!-- ConfigurationProperties属性配置工具依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <!-- json依赖 -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 处理 ConfigurationProperties报错 -->
            <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>
</project>

(2). 样例之:person.java

package com.niewj.springboot.model;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niewj on 2020/8/5 0:01
 */
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private Boolean male;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;
}

(3). Dog.java

package com.niewj.springboot.model;

import lombok.Data;

/**
 * Created by niewj on 2020/8/5 0:04
 */
@Data
public class Dog {
    private String name;
    private Integer age;
}

(4). application.yml

server:
  port: 8888

person:
  lastName: 张三
  age: 33
  male: true
  birth: 1985/03/03
  maps: {k1: v1, k2: 20, k3: true}
  lists:
    - lisi
    - wangwu
  dog:
    name: 小黑
    age: 3

(5). 测试用例:

package com.niewj.springboot;

import com.google.gson.Gson;
import com.niewj.springboot.model.Person;
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;

/**
 * Created by niewj on 2020/8/5 0:19
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestProperties {

    @Autowired
    private Person person;

    @Test
    public void printPerson(){
        System.out.println(new Gson().toJson(person));
    }
}

(6). 输出:

{
    "lastName": "张三",
    "age": 33,
    "male": true,
    "birth": "Mar 3, 1985 12:00:00 AM",
    "maps": {
        "k1": "v1",
        "k2": 20,
        "k3": true
    },
    "lists": ["lisi", "wangwu"],
    "dog": {
        "name": "小黑",
        "age": 3
    }
}

3.2 @ConfigurationProperties 和 @Value比较

3.2.1 @ConfiguratonProperties

  1. 适合场景: 批量注入配置文件中的属性;

  2. 支持 Relax-Binding(送伞绑定);

    Person.firstName 匹配:

    person.first-name

    person.first_name

    person.firstName

    PERSON_FIRST_NAME

  3. 不支持spEL表达式;

  4. JSR303 数据校验支持;

  5. 支持复杂类型: 可以取到 maps 的值;

3.2.2 @Value

  1. 适合场景: 一个一个指定配置属性;

  2. 不支持 Relax-Binding (松散绑定);

  3. 支持spEL;

    如 @Value(“#{20*2}”) // 会计算出 40赋值给字段属性

  4. 不支持JSR303数据校验;

  5. @Value取不到 maps 的值(不支持复杂类型)


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 hi@niewj.com

×

喜欢就点赞,疼爱就打赏