上一節: SpringBoot開發筆記-(2) springboot原理初探-解析helloworldjava
@Component+@ConfigurationProperties(prefix="person")
web
person是在yml中配置的前綴: person: ...spring
(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 } }
支持 Relax-Binding(送傘綁定);maven
如 Person.firstName
匹配:spring-boot
person.first-nameperson.first_name
person.firstName
PERSON_FIRST_NAME
如 @Value("#{20*2}") // 會計算出 40賦值給字段屬性