springboot(二)外部配置

springboot 配置信息

springboot中 属性配置
  • yml/properties 文件配置: name:springboot
  • command-line java -jar xxx.jar --name="springboot"
程序中获取方式
@component
    public class PropertiesConfiguration{
        
        @Value("${name}")
        private String name;
        
        @PostConstruct
        public void init(){
            System.out.println(name); // springboot
        }
    }
springboot properties 中定义随机数
//yml or properties 方式
my.secret: ${random.value}
my.number: ${random.int}
my.bignumber: ${random.long}
my.uuid: ${random.uuid}
my.number.less.then.ten: ${random.int(10)} //获取10之内的数据
my.number.in.range: ${random.int[1024,1040]} //获取范围数据

//命令行 方式
java -jar xx.jar --my.secret='${random.uuid}'


命令行方式中参数java

  • 必须的使用 单引号,不然报错。
  • -- 表示经过命令行方式设置 环境属性;
  • 命令行方式 级别最高;
  • SpringApplication.setAddCommandLineProperties(false) 表示关闭命令行方式
配置文件加载:
properties or yml 加载前后顺序
  • jar包同级目录 /config 下;
  • jar包同级目录;
  • classpath /config
  • classpath
指定 properties or yml 文件

springboot默认加载的是 application.properties or application.yml 文件; command-line 修改默认加载配置文件 --spring.config.name=myApplication
command-line 指定多个配置文件 --spring.config.name='classpath:/myapplication.properties,classpath:/showapplication.properties'
command-line 指定文件加载目录: --spring.config.location='classpath:/,file:./' 加载顺序是从后往前加载;spring

properties or yml 默认加载 default文件

除加载 application.yml 文件之外,还会加载 application-default.yml 文件 优先使用 -default 配置信息,若是不存在才会从 application.yml 中加载;
能够使用 spring.profiles.active=dev 指定,则默认加载的指定文件变为:application-dev.yml;springboot

直接赋值给 对象、Map、list
配置文件中赋值
//赋值到Map中 同理赋值到 Bean中
@ConfigurationProperties(profix='my')
public class ConfigurationBean{
    private Map<String, String> prod;
    private List<String> servers;
    public void setProd(HashMap<String, String> prod) {
        this.prod = prod;
    }
    public void setServers(List<String> servers){
        this.servers = servers;
    }
}

//配置文件赋值方式
my:
  dev:
    url: http://dev.example.com
    name: Developer setup
  prod:
    url: http://prod.example.com
    name: My CollMap
  servers:
    - http://www.baidu.com
    - http://www.google.com

//打印结果:
prod: {name=My CollMap, url=http://prod.example.com}
servers: [http://www.baidu.com, http://www.google.com]
    
//command-line 赋值方式
java -jar xxx.jar --my.prod.url='http://core.com' --my.prod.name='core name'
//打印结果
prod: {name=core name, url=http://core.com}
java -jar xxx.jar --my.servers='[http://core.com,http://www.my.com]'
servers: [http://core.com, http://www.my.com]
单个文件中配置多个环境

在 yml 单个文件中配置多个环境信息 使用 --- 进行分割;
spring.profiles.active= development 表示激活那个配置 spring.profiles 表示当前的 配置分类
当前内容必须的再同一个yml文件当中
例如demo:当前启动的端口为 8081app

spring:
  profiles:
    active: development
server:
    port: 8080
---
spring:
    profiles: development
server:
    port: 8081
---
spring:
    profiles: production
server:
    port: 8082
配置文件内容导入到 类方式
@Configuration
public class EnablePropertiesConfiguration {

    @ConfigurationProperties(prefix = "environments")
    @Bean
    public PropertiesConfigurationOther propertiesConfigurationOther(){
        return new PropertiesConfigurationOther();
    }
    
    
}

public class PropertiesConfigurationOther {

    @Value("${name}")
    private String value;


    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
properties yml 轻松绑定

- 方式, spring.first-name 能够使用 spring.firstname
大小写 方式,配置文件中,不区分大小写 推荐使用 - 方式less

properties yml 给List<Bean> 赋值
@Getter
@Setter
public class MyBean {
    private String name;
    private String desc;
}

application.yml 

environments:
  myBeans:
    - name: zhsnagsan
      desc: shangsan-desc
    - name: lisi
      desc: lisi-desc
      
@Component
@ConfigurationProperties(prefix = "environments")
public class PropertiesConfiguration {
    private List<MyBean> myBeans;
    public void setMyBean(List<MyBean> myBeans){
        this.myBeans = myBeans;
    }
}
对参数进行验证
//类上添加 @Validated注解
// 对应的字段上添加 @NotNull
//当前若是没有在 yml中配置 myBeans 中的内容 ,致使myBeans为null 则会报错

......

@Component
@ConfigurationProperties(prefix = "environments")
@Validated
public class PropertiesConfiguration {
    @NotNull
    private List<MyBean> myBeans;
    public void setMyBean(List<MyBean> myBeans){
        this.myBeans = myBeans;
    }
}
相关文章
相关标签/搜索