2024.4.23 Tuesday
接上文【WEEK9】 【DAY1】YAML配置注入第一部分【中文版】
3.3.4.通过yaml赋值
3.3.4.1.修改Dog.java(去除赋值的相关语句)
package com.p8.springboot02config.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component //添加到Spring的组件里(注册bean)
public class Dog {
private String name;
private Integer age;
//快捷键Alt+Insert
public Dog(){
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.3.4.2.修改Person.java(在第11行添加@ConfigurationProperties)
package com.p8.springboot02config.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component //注册bean
@ConfigurationProperties(prefix = "person")
/*
@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
*/
public class Person {...}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
将图中红框部分的依赖粘贴到pom.xml文件依赖部分中,刷新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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.P8</groupId>
<artifactId>springboot-02-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-02-config</name>
<description>springboot-02-config</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.3.4.3.修改application.yaml
person:
name: zhangsan
age: 1
happy: false
birth: 2024/01/01
maps: {k1: v1,k2: v2}
lists:
- programming
- sports
- laws
dog:
name: wangcai
age: 0
3.3.4.4.修改Springboot02ConfigApplicationTests.java进行测试
package com.p8.springboot02config;
import com.p8.springboot02config.pojo.Dog;
import com.p8.springboot02config.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot02ConfigApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
3.3.5.通过properties赋值
3.3.5.1.新建zhangsan.properties
name=zhangsan
3.3.5.2.修改Person.java
package com.p8.springboot02config.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component //注册bean
//@ConfigurationProperties(prefix = "person")
/*
@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
*/
//加载指定位置的指定配置文件
@PropertySource(value = "classpath:zhangsan.properties")
@ConfigurationProperties //默认从全局配置文件中获取值,不加这行则输出时name='null'(zhangsan.properties没被扫描)
public class Person {
//若不使用@ConfigurationProperties,也可以通过SPEL表达式取出配置文件的值
@Value("${name}")
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public Person(){
}
public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getHappy() {
return happy;
}
public void setHappy(Boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
3.3.5.3.运行Springboot02ConfigApplicationTests.java进行测试
3.3.6.配置文件占位符
3.3.6.1.修改application.yaml
person:
name: zhangsan${random.uuid} #随机uuid
age: ${random.int} #随机int
happy: false
birth: 2024/01/01
maps: {k1: v1,k2: v2}
lists:
- programming
- sports
- laws
hello: world
dog:
name: ${person.hello:hi}_wangcai #当person.hello不存在时,赋成:右边的默认值(此处是hi)
age: 0
3.3.6.2.修改Person.java回3.3.4.的版本
3.3.6.3.运行Springboot02ConfigApplicationTests.java进行测试
3.3.7.对比@Value和@ConfigurationProperties
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件中的属性 | 逐个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
松散绑定:在yaml文件中命名为abc-def,对应java文件中的名称使用驼峰命名AbcDef,在运行时仍然可以将这两者成功对应起来。
https://blog.csdn.net/A_art_xiang/article/details/134370029
- @ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加
- 松散绑定:这个什么意思呢? 比如我的yml中写的last-name,这个和lastName是一样的, - 后面跟着的字母默认是大写的。这就是松散绑定。可以测试一下
- JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性
- 复杂类型封装,yml中可以封装对象 , 使用value就不支持
- 结论:
- 配置yml和配置properties都可以获取到值,强烈推荐 yml;
- 如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下@value;
- 如果说,我们专门编写了一个JavaBean来和配置文件进行一一映射,就直接@configurationProperties,不要犹豫!
配置文件读取顺序:yml -> yaml -> properties,所以,如果都配置了同名的配置,以properties为准,因为后读取的会覆盖前面的