SpringBoot開發筆記-(3) 屬性文件讀取1: @ConfigurationProperties讀取yml配置文件

上一節: SpringBoot開發筆記-(2) springboot原理初探-解析helloworldjava

3.1 ConfigurationProperties使用方式

3.1.1. 步驟

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

person是在yml中配置的前綴: person: ...spring

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.javaapache

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.javajson

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.ymlsegmentfault

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). 測試用例:springboot

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). 輸出:app

{
    "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(送傘綁定);maven

    Person.firstName 匹配:spring-boot

    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 的值(不支持複雜類型)
相關文章
相關標籤/搜索