6.2 SpringBoot的核心:外部配置

6.2 SpringBoot的核心:外部配置

6.2 SpringBoot的核心:外部配置_第1张图片

6.2 外部配置

SpringBoot允许使用
1.properties
2.Yaml
3.命令行
作为外部参数

6.2.1 命令行参数配置

1.java -jar xx.jar --server.port=9090

6.2.2 常规属性配置

1.在Spring的情况下,注入propertis需要@PropertySource指明properties文件的位置,然后使用@value
2.SpringBoot中,直接使用@Value就行(应该是指定只有一个properties)

举例:

properties
mouth: 11
day: 12
##配置使用配置
time: 2017-${mouth}-${day}

注入
@Value("${time}")
private String time;

6.2.3 类型安全属性配置

可以通过@ConfigurationProperties来完成一个统一的注入类

实例:

vo
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "person")
public class PersonInstance {
    private String name;
    private Integer age;
    private String address;
}

properties
##对象配置
person:
  name: 王海潮
  age: 22
  address: 小上海

你可能感兴趣的:(SpringBoot完整版)