SpringBoot系列:2、配置

2019-10-17  本文已影响0人  程序员小H

1、配置文件

1.1 配置文件

SpringBoot使用一个全局的配置文件

1.2 YAML

1.2.1 基本语法
server:
  port: 8081
1.2.2 值的写法

2、读取配置文件

2.1 第一种@ConfigurationProperties 方式

application.yml文件内容

person:
  lastName: xiaoH
  age: 29
  boss: false
  birth: 2019-10-16
  maps: {k1: v1, k2: v2}
  lists:
    - zhangsan
    - lisi
    - wangwu
  dog:
    name: 旺财
    age: 2

Dog.java

public class Dog {

    private String name;
    private Integer age;
      
        // 省略getter和setter方法
}

Person.java

@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 dog;

    // 省略getter和setter方法
}

注意:使用@ConfigurationProperties(prefix = "person")注解,会提示


file

这个不是报错,只是idea提示,在pom文件中引入下面这个jar即可。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
</dependency>

单元测试类SpringbootConfigApplicationTests.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootConfigApplicationTests {

    @Autowired
    Person person;

    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}

运行单元测试类输出:


file

2.2 第三种@Value方式

配置文件不变,
Person.java

@Component
//@ConfigurationProperties(prefix = "person")
public class Person {

    @Value("${person.lastName}")
    private String lastName;

    @Value("#{14*2}")
    private Integer age;

    @Value("true")
    private Boolean boss;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

    // 省略掉getter、setter、toString方法
}

运行单元测试类输出:


file

注意:使用@Value注解需要保证所包含的键名在配置文件中是一定要存在的,否则会报错。比如注释掉配置文件中的lastName属性,然后在执行单元测试,报错如下:


file

解决方式:

@Value("${person.lastName:null}")
    private String lastName;
file

2.3 @ConfigurationProperties与@Value的区别

对比项 @ConfigurationProperties @Value
功能 批量注入配置文件中的属性 一个个的绑定
松散绑定 支持 不支持
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持

2.4 数据校验

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

//    @Value("${person.lastName:null}")
    @Email
    private String lastName;

//    @Value("#{14*2}")
    private Integer age;

//    @Value("true")
    private Boolean boss;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;
}

2.5 @PropertySource

加载指定的配置文件
新建一个person.properties

person.last-name=xiaoH666
person.age=29
person.boss=false
person.birth=2019/11/11
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,s,d
person.dog.name=wangcai
person.dog.age=2

Person.java

@PropertySource(value = "classpath:person.properties")
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {

//    @Value("${person.lastName:null}")
//    @Email
    private String lastName;

//    @Value("#{14*2}")
    private Integer age;

//    @Value("true")
    private Boolean boss;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

    // /省略掉getter、setter、toString方法
}

执行单元测试,输出结果


file

3、源码

GitHub:https://github.com/chenjiecg/SpringBoot.git

本文由博客一文多发平台 OpenWrite 发布!

上一篇 下一篇

猜你喜欢

热点阅读