SpringBoot属性配置文件详解

SpringBoot属性配置文件详解

1.1属性与加载

1.1简单属性的加载

List类型

  • properties文件中使用[]来定位列表类型,比如:
spring.my-example.url[0]=http://example.com
spring.my-example.url[1]=http://spring.io

#也支持使用逗号分割的配置方式,上面与下面的配置是等价的:
spring.my-example.url=http://example.com,http://spring.io
  • yaml文件中使用可以使用如下配置:
spring:
  my-example:
    url:
      - http://example.com
      - http://spring.io

#也支持逗号分割的方式:
spring:
  my-example:
    url: http://example.com, http://spring.io

注意:在Spring Boot 2.0中对于List类型的配置必须是连续的,不然会抛出UnboundConfigurationPropertiesException异常,所以如下配置是不允许的:

foo[0]=a
foo[2]=b

在Spring Boot 1.x中上述配置是可以的,foo[1]由于没有配置,它的值会是null

Map类型

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

spring.my-example.foo=bar
spring.my-example.hello=world

yaml格式:

spring:
  my-example:
    foo: bar
    hello: world

注意:如果Map类型的key包含非字母数字和-的字符,需要用[]括起来,比如:

spring:
  my-example:
    '[foo.baz]': bar

List类型

1.2自定义属性的加载

application-properties配置自定义属性

com.blog.name=小明
com.blog.title=Spring Boot教程
com.blog.desc=${com.didispace.blog.name}学习《${com.didispace.blog.title}》

实体类

@Component
public class BlogProperties {

    @Value("${com.blog.name}")
    private String name;
    @Value("${com.blog.title}")
    private String title;
    @Value("${com.blog.desc}")
    private String desc;

    //省略getter和setter方法。
    }

1.2属性的读取

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

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

当要读取配置文件中spring.jpa.database-platform的配置,可以这样写:

this.environment.containsProperty("spring.jpa.database-platform")

而下面的方式是无法获取到spring.jpa.database-platform配置内容的:

this.environment.containsProperty("spring.jpa.databasePlatform")

注意:使用@Value获取配置内容的时候也需要这样的特点

1.3全新的绑定API

在Spring Boot 2.0中增加了新的绑定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());
    }
}

例子二:List类型

com.didispace.post[0]=Why Spring Boot
com.didispace.post[1]=Why Spring Cloud

com.didispace.posts[0].title=Why Spring Boot
com.didispace.posts[0].content=It is perfect!
com.didispace.posts[1].title=Why Spring Cloud
com.didispace.posts[1].content=It is perfect too!

实现:

ApplicationContext context = SpringApplication.run(Application.class, args);

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

// 绑定List配置
List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();
System.out.println(post);

List<PostInfo> posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get();
System.out.println(posts);

2.多环境配置

这里写图片描述

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

application-properties环境配置文件

 # 多环境配置文件激活属性
spring.profiles.active=dev

3.

猜你喜欢

转载自blog.csdn.net/c_royi/article/details/81281108
今日推荐