springboot给属性赋值的三种方式:
第一种:@Value
@Value()只可以给普通变量赋值,不能直接给静态变量赋值
package com.peng.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Dog {
@Value("小黄")
private String name;
@Value("3")
private Integer age;
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 +
'}';
}
}
package com.peng;
import com.peng.pojo.Dog;
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 Dog dog;
@Test
void contextLoads() {
System.out.println(dog);
}
}
第二种:使用yaml的方式
- 使用
yaml配置文件与实体类通过:@ConfigurationProperties(prefix = “person”)注解可以进行绑定。
@ConfigurationProperties作用有那些:
- 将配置文件中配置的每一个属性的值,映射到这个组件中。
- 告诉Springboot 将本类中的所有属性和配置文件中相关的配置进行绑定
- 参数 prefix = “person” :将配置文件中的person下面的所有属性一一对应
- 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
实例:
- 简单的建立个Person类:
package com.peng.pojo;
import org.springframework.beans.factory.annotation.Value;
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")
public class Person {
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 +
'}';
}
}
- 配置ymal文件:
Person:
name: cuiepng
age: 23
happy: false
birth: 2020/1/13
maps: {k1,v1,k2,v2}
lists: [code,music,girl]
dog:
name: 旺财2
age: 13
- 测试类:
package com.peng;
import com.peng.pojo.Dog;
import com.peng.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);
}
}
测试结果:

第三种:使用绑定配置文件的形式
package com.peng.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
//javaConfig 绑定我们配置文件的值,可以采取这种方式!
//加载只当的配置文件
@PropertySource(value = "classpath:cuipeng.properties")
public class Person {
//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 +
'}';
}
}
建立.properties文件:

输出:
package com.peng;
import com.peng.pojo.Dog;
import com.peng.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SpringBootTest
class Springboot02ConfigApplicationTests{
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
结果:

@ConfigurationProperties和@Value对比:

总结:
- 配置 ymal 和配置 properties 都可以获取到值,强烈推荐 ymal.
- 如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用以下 @value.
- 如果专门编写一个javabean来和配置文件进行映射,就直接使用 @ConfigurationProperties,不能犹豫。
本文详细介绍了Spring Boot中三种属性注入的方式:@Value、YAML配置以及使用@ConfigurationProperties。@Value适用于简单属性注入,YAML配置允许绑定复杂对象,而@ConfigurationProperties则能将配置文件中的所有相关属性映射到一个类上。推荐使用YAML配置,因为它更直观易读。对于只需要获取单一配置值的情况,@Value注解更为便捷。
977

被折叠的 条评论
为什么被折叠?



