SpringBoot 读取yml配置文件信息
<!--导入配置文件处理器,配置文件绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
直接绑定对象
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix ="go-fastdfs")
@Data
public class GofastdfsValue {
private Stringurl;
private Stringgroup;
private StringreplaceUrl;
}
直接绑定
@Value("${go-fastdfs.url}")
private StringgofastdfsUrl;
@Value("${go-fastdfs.group}")
private StringgofastdfsGroup;
@Value("${go-fastdfs.replace-url}")
private StringgofastdfsReplaceUrl;
绑定的值含有list,逗号分隔
person.last-name=沈宁
person.age=22
person.boss=false
person.brith=2019/12/01
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c,d,e,e,p
person.dog.name=宗臣
person.dog.age=18
区别@Data
@Component
public class Persons {
//获取配置文件中的属性
@Value("${person.last-name}")
private String lastName;
//直接给变量赋值
@Value("#{11*2}")
private Integer age;
@Value("${person.boss}")
private boolean boss;
@Value("${person.brith}")
private Date brith;
// @Value 不支持
private Map<String,Object> maps;
@Value("${person.lists}")
private List<String> lists;
private Dog dog;
}
指定加载对象
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Date brith;
private Map<String,Object> maps;
private List<String> lists;
private Dog dog;
}