目录
@ConfigurationProperties 与 @Value比较
@Value(“”)介绍:
@Value(“”)介绍:用来指定bean中某一个属性的值,它的值可以是字面量,从配置文件获取的值,spel表达式
例如:
@Value("${person.last-name}")
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value("true")
private Boolean boss;
配置文件做如下配置:
person.last-name=张三
@configurationProperties:
@configurationProperties:从配置文件中读取所有的属性的值;
例如:
package com.example.springboot02config.bean;
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;
/**
* 将配置文件中配置的每一个属性的值映射到这个组件中
*@ConfigurationProperties:这个注解的作用是告诉spring boot本类中的所有属性和配置文件中的配置进行绑定;
* prefix = "person";指定与哪一个下面的属性进行映射
* 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
* @Component:把这个bean加入到容器中
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dogs;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void set