Files
notes_estom/Spring/Springboot/2配置.md
2020-07-21 09:13:01 +08:00

6.1 KiB
Raw Blame History

1 配置基础

默认配置文件

用于配置容器端口名、数据库链接信息、日志级别。pom是项目编程的配置properties是软件部署的配置。

src/main/resources/application.properties

yaml配置文件实例

environments:
    dev:
        url: http://dev.bar.com
        name: Developer Setup
    prod:
        url: http://foo.bar.com
        name: My Cool App

等价的properties配置文件

environments.dev.url=http://dev.bar.com
environments.dev.name=Developer Setup
environments.prod.url=http://foo.bar.com
environments.prod.name=My Cool App

yaml的自定义参数

  • 定义自定义的参数
book.name=SpringCloudInAction
book.author=ZhaiYongchao
  • 通过占位符的方式加载自定义的参数
@Component
public class Book {

    @Value("${book.name}")
    private String name;
    @Value("${book.author}")
    private String author;

    // 省略getter和setter
}
  • 通过SpEL表达式加载自定义参数

''' #{...} '''

使用随机数配置

${random}的配置方式主要有一下几种,读者可作为参考使用。

# 随机字符串
com.didispace.blog.value=${random.value}
# 随机int
com.didispace.blog.number=${random.int}
# 随机long
com.didispace.blog.bignumber=${random.long}
# 10以内的随机数
com.didispace.blog.test1=${random.int(10)}
# 10-20的随机数
com.didispace.blog.test2=${random.int[10,20]}

通过命令行配置

在启动java应用是添加配置参数

java -jar xxx.jar --server.port=8888

2 多环境配置

配置方法

对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包。

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式其中{profile}对应你的环境标识,比如:

  • application-dev.properties开发环境
  • application-test.properties测试环境
  • application-prod.properties生产环境

配置加载顺序

  1. 命令行中传入的参数。
  2. SPRING_APPLICATION_JSON中的属性。SPRING_APPLICATION_JSON是以JSON格式配置在系统环境变量中的内容。
  3. java:comp/env中的JNDI属性。
  4. Java的系统属性可以通过System.getProperties()获得的内容。
  5. 操作系统的环境变量
  6. 通过random.*配置的随机属性
  7. 位于当前应用jar包之外针对不同{profile}环境的配置文件内容例如application-{profile}.properties或是YAML定义的配置文件
  8. 位于当前应用jar包之内针对不同{profile}环境的配置文件内容例如application-{profile}.properties或是YAML定义的配置文件
  9. 位于当前应用jar包之外的application.properties和YAML配置内容
  10. 位于当前应用jar包之内的application.properties和YAML配置内容
  11. 在@Configuration注解修改的类中通过@PropertySource注解定义的属性
  12. 应用默认属性使用SpringApplication.setDefaultProperties定义的内容1.

3 配置文件属性绑定

Spring Boot 2.0 新特性

  • 移除特殊字符
  • 全小写

列表类型

必须使用连续下标索引进行配置。

  • properties中使用[]在定位列表类型
pring.my-example.url[0]=http://example.com
spring.my-example.url[1]=http://spring.io
  • properties中使用,分割列表类型。
pring.my-example.url[0]=http://example.com
spring.my-example.url[1]=http://spring.io
  • yaml中使用列表
spring:
  my-example:
    url:
      - http://example.com
      - http://spring.io

  • yaml文件中使用,分割列表
spring:
  my-example:
    url: http://example.com, http://spring.io

Map类型

Map类型在properties和yaml中的标准配置方式如下

  • properties格式
spring.my-example.foo=bar
spring.my-example.hello=world
  • yaml格式
spring:
  my-example:
    foo: bar
    hello: world

4 环境属性绑定

简单类型

在环境变量中通过小写转换与.替换_来映射配置文件中的内容比如环境变量SPRING_JPA_DATABASEPLATFORM=mysql的配置会产生与在配置文件中设置spring.jpa.databaseplatform=mysql一样的效果。

List类型

由于环境变量中无法使用[和]符号所以使用_来替代。任何由下划线包围的数字都会被认为是[]的数组形式。

MY_FOO_1_ = my.foo[1]
MY_FOO_1_BAR = my.foo[1].bar
MY_FOO_1_2_ = my.foo[1][2]

5 系统属性绑定

简单类型

系统属性与文件配置中的类似,都以移除特殊字符并转化小写后实现绑定

list类型

系统属性的绑定也与文件属性的绑定类似,通过[]来标示,比如:

-D"spring.my-example.url[0]=http://example.com"
-D"spring.my-example.url[1]=http://spring.io"

同样的,他也支持逗号分割的方式,比如:

-Dspring.my-example.url=http://example.com,http://spring.io

6 属性读取

在Spring应用程序的environment中读取属性的时候每个属性的唯一名称符合如下规则

  • 通过.分离各个元素
  • 最后一个.将前缀与属性名称分开
  • 必须是字母a-z和数字(0-9)
  • 必须是小写字母
  • 用连字符-来分隔单词
  • 唯一允许的其他字符是[和]用于List的索引
  • 不能以数字开头
this.environment.containsProperty("spring.jpa.database-platform")

7 新的绑定API

简单类型

假设在propertes配置中有这样一个配置

com.didispace.foo=bar

我们为它创建对应的配置类:

@Data
@ConfigurationProperties(prefix = "com.didispace")
public class FooProperties {

    private String foo;

}

接下来通过最新的Binder就可以这样来拿配置信息了

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Application.class, args);

        Binder binder = Binder.get(context.getEnvironment());

        // 绑定简单配置
        FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get();
        System.out.println(foo.getFoo());
    }
}