SpringBoot基础篇配置信息之如何读取配置信息

2019-10-08 15:35:56

参考资料 SpringBoot基础篇配置信息之如何读取配置信息

更多Spring文章,欢迎点击 一灰灰Blog-Spring专题


SpringBoot极大的减少了配置,开一个新项目时,完全可以做到什么配置都不加,就可以直接跑,简单方便的同时,就带来了一个问题


怎么知道这些默认的配置是什么?

如果要修改默认配置怎么办?

如何添加自定义的配置?

如何读取这些配置?

I. 配置信息读取

首先创建一个SpringBoot项目,这一块就直接省略掉,下面直奔主题,如何获取配置


1. 配置文件

默认读取配置文件 application.properties 或者 application.yml 中的配置信息,两种不同的文件类型,对应的内部配置方式也不太一样


配置文件位置


一般来说,默认的配置文件application.properties或者application.yml文件放在目录


src/main/resources/

1

properties格式


properties配置文件属于比较常见的一种了,定义也比较简单,形如 key=value,一个实例如下


#服务端口号

server.port=8081


app.proper.key=${random.uuid}

app.proper.id=${random.int}

app.proper.value=test123


app.demo.val=autoInject

1

2

3

4

5

6

7

8

yml格式


yml格式的配置文件是以缩进来表示分层,kv之间用冒号来分割,形如


#服务端口号

server:

  port: 8081


app:

  proper:

    key: ${random.uuid}

    id: ${random.int}

    value: test123


  demo:

    val: autoInject

1

2

3

4

5

6

7

8

9

10

11

12

格式对比


两种不同格式的配置文件,有啥区别?


单纯从使用来讲,并没有特别的不同,而且我个人也一直这么认为的,直到遇到了一个诡异的问题,后面给出


2. 配置读取

程序启动之后,如何获取配置文件application.yml中的配置信息呢?在实际的使用中,最常见的有三种姿势


a. Environment 读取

所有的配置信息,都会加载到Environment实体中,因此我们可以通过这个对象来获取系统的配置,通过这种方式不仅可以获取application.yml配置信息,还可以获取更多的系统信息


使用姿势如下:


@RestController

public class DemoController {

    @Autowired

    private Environment environment;


    @GetMapping(path = "show")

    public String show() {

        Map<String, String> result = new HashMap<>(4);

        result.put("env", environment.getProperty("server.port"));

        return JSON.toJSONString(result);

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

b. @Value 注解方式

@Value注解可以将配置信息注入到Bean的属性,也是比较常见的使用方式,但有几点需要额外注意


如果配置信息不存在会怎样?

配置冲突了会怎样(即多个配置文件中有同一个key时)?

使用方式如下,主要是通过 ${},大括号内为配置的Key;如果配置不存在时,给一个默认值时,可以用冒号分割,后面为具体的值


@RestController

public class DemoController {

    // 配置必须存在,且获取的是配置名为 app.demo.val 的配置信息

    @Value("${app.demo.val}")

    private String autoInject;


    // 配置app.demo.not不存在时,不抛异常,给一个默认值data

    @Value("${app.demo.not:dada}")

    private String notExists;


    @GetMapping(path = "show")

    public String show() {

        Map<String, String> result = new HashMap<>(4);

        result.put("autoInject", autoInject);

        result.put("not", notExists);

        return JSON.toJSONString(result);

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

c. 对象映射方式

上面的两种方式对于某几个特别的配置来说,一个一个的写还好,如果配置特别多时,每一个都去这么玩,估计会敲的键盘原地爆炸了,当然这么不友好的事情,怎么能忍!因此就有了下面这种使用方式


@Data

@Component

@ConfigurationProperties(prefix = "app.proper")

public class ProperBean {

    private String key;

    private Integer id;

    private String value;

}

1

2

3

4

5

6

7

8

上面的写法,含义是将配置文件中配置 app.proper.key, app.proper.id, app.proper.value三个配置的值,赋值给上面的bean


即通过注解ConfigurationProperties来制定配置的前缀

通过Bean的属性名,补上前缀,来完整定位配置信息的Key,并获取Value赋值给这个Bean

上面这个过程,配置的注入,从有限的经验来看,多半是反射来实现的,所以这个Bean属性的Getter/Setter方法得加一下,上面借助了Lombok来实现,标一个@Component表示这是个Bean,托付给Spring的ApplicationConttext来管理


3. 读取测试

配置文件application.properties信息如下


#服务端口号

server.port=8081


app.proper.key=${random.uuid}

app.proper.id=${random.int}

app.proper.value=test123


app.demo.val=autoInject


user.name=一灰灰Blog

1

2

3

4

5

6

7

8

9

10

写一个DemoController来返回读取的配置值


@RestController

public class DemoController {

    @Autowired

    private Environment environment;

    @Autowired

    private ProperBean properBean;


    @Value("${app.demo.val}")

    private String autoInject;


    @Value("${app.demo.not:dada}")

    private String notExists;


    @Value("${user.name}")

    private String name;


    @GetMapping(path = "show")

    public String show() {

        Map<String, String> result = new HashMap<>(6);

        result.put("properBean", properBean.toString());

        result.put("autoInject", autoInject);

        result.put("env", environment.getProperty("server.port"));

        result.put("not", notExists);

        result.put("name", name);

        return JSON.toJSONString(result);

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

访问后输出如下


{

    "autoInject": "autoInject",

    "name": "user",

    "not": "dada",

    "env": "8081",

    "properBean": "ProperBean(key=d4f49141-fa67-4e4c-9e23-c495ff02fda7, id=132483528, value=test123)"

}

1

2

3

4

5

6

7

请注意上面的not 和 name返回


属性notExists对应的配置信息,在配置文件中没有定义,所以返回默认的data

属性name对应的配置信息 user.name 在application.properties文件中是一灰灰Blog,但是返回了user(测试环境为mac,mac系统的用户名为user,为啥叫user?因为某某人…)

造成这个的根源是application.properties的配置被更高优先级的系统配置覆盖了

4. 小结

前面主要介绍了常见的三种获取配置信息的方式,但遗留了几个问题


配置信息读取的优先级问题(为什么 user.name 配置被覆盖?)

如何读取其他配置文件如 xxx.properties 的配置信息(能读取么?)

配置文件中的 ${random.int} 是什么鬼?

SpringBoot的默认配置是些啥


  • 2017-02-10 15:24:14

    linux学习之——vim简明教程

    学习 vim 并且其会成为你最后一个使用的文本编辑器。没有比这个更好的文本编辑器了,非常地难学,但是却不可思议地好用。 我建议下面这四个步骤: 存活 感觉良好 觉得更好,更强,更快 使用VIM的超能力

  • 2017-02-10 16:22:13

    git历史记录查询

    查看提交历史:git log 查看提交历史并显示版本间的差异:git log -p 查看指定历史:git log xxx(sha1值) -p 查看提交历史(指定时间):

  • 2017-02-13 17:50:05

    cURL error 60: SSL certificate problem: unable to get local issuer certificate

    Drupal 8 version uses Guzzle Http Client internally, but under the hood it may use cURL or PHP internals. If you installed PHP cURL on your PHP server it typically uses cURL and you may see an exception with error Peer certificate cannot be authenticated with known CA certificates or error code CURLE_SSL_CACERT (60).

  • 2017-02-16 08:09:01

    HTML中PRE和p的区别

    pre 元素可定义预格式化的文本。被包围在 pre 元素中的文本通常会保留空格和换行符。而文本也会呈现为等宽字体。 <pre> 标签的一个常见应用就是用来表示计算机的源代码。