【SpringBoot-02】配置类

尚硅谷SpringBoot视频 P10~

配置:通过如下其中一种中参数定义,可修改springboot中默认的参数

1、application.properties

如:server.port=8081

2、application.yaml,配置样例

server:

  port: 8081

应用示例:

 1 public class Person {
 2     private String lastName;
 3     private Integer age;
 4     private Boolean boss;
 5     private Date birth;
 6 
 7     private Map<String ,Object> maps;
 8     private List<Object> lists;
 9     private Dog dog;
10 
11     ....
12 }
13 
14 public class Dog{
15     private String name;
16     private Integer age;
17     ... 
18 }

yaml配置如下:

 1 person:
 2     lastName: hello
 3     age: 18
 4     boss: false
 5     birth: 2017/12/12
 6     maps:{k1: v1, k2: 12}
 7     lists:
 8         - lisi
 9         - zhaoliu
10     dog:
11         name: 小狗
12         age: 12

改造:

 
 

/**
* 将配置文件中配置的每个属性的值,映射到这个组件中
* @ConfigurationProperties 告诉 SpringBoot将本类中所有属性和配置文件中相关配置绑定
* prefix = "person" 配置文件中哪个下面的所有属性一一映射
*/
@Component
@ConfigurationPeroperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;

 

猜你喜欢

转载自www.cnblogs.com/clarino/p/12001439.html
今日推荐