【Spring Boot 】Spring Boot 常用配置总结

矫情吗;* 2024-02-05 12:30 105阅读 0赞

文章目录

  • 前言
  • 1.多环境配置
    • application.properties
    • application.yaml
  • 2.常用配置
  • 3.配置读取
  • 4.自定义配置

前言

在涉及项目开发时,通常我们会灵活地把一些配置项集中在一起,如果你的项目不是很大的情况下,那么通过配置文件集中不失为一个很好的解决方案。
在 Spring Boot 中,我们可以方便地通过读取 appliction.properties/application.yaml 格式的配置文件,进而注入我们的项目中。

1.多环境配置

在日常开发中,我们的环境根据不同的阶段会有一定不同,总的可以分为:dev、test、prod,举个简单的例子,不同的环境中如 port 可能是不同的。
上文中提到配置文件多以 appliction.properties/application.yaml 这两种格式为主,下面分别就这两种格式对多环境的配置做个说明。

application.properties

在这种格式中,主配置文件是 application.properties,对于不同环境的配置,通常我们会命名为 application-xxx.properties,这里的 xxx 可以是 dev、test、prod 中一种,比如我们的 主配置文件(appliction.properties) 内容如下:
properties

  1. server.port
  2. spring.profiles.active=dev

这样配置读取时,就会去 application-dev.properties 中读取相关的配置,其他同理。

application.yaml

熟悉 yaml 的小伙伴对其格式肯定不陌生,通常都是同级内容对齐,分级项通过另启一行,且通常需要固定的空格缩进,一般是2个空格,这里不多说,格式问题自行搜索。
我们假定主配置文件是 application.yaml,其他环境的配置文件是:application-dev.yaml/application-test.yaml/application-prod.yaml 。
这里我们看看在看看 yaml 主配置文件中怎么配置:

  1. server:
  2. port: 8080
  3. spring:
  4. profiles:
  5. active: dev

2.常用配置

通常我们的配置应该包含如下选项:

  • server运行配置,如端口,ip,是否SSL,超时时间,多线程等
  • server的介绍信息配置
  • 日志配置信息,又或者日志部分单独配置
  • 数据库的配置信息
  • 缓存的配置信息

比如,我们的配置项可以是以下信息:

  1. # server
  2. server:
  3. port: 8000
  4. tomcat:
  5. threads:
  6. max: 10
  7. min-spare: 3
  8. uri-encoding: UTF-8
  9. # self define
  10. app:
  11. name: springDemo
  12. desc: a-spring-boot-app
  13. version: 1.0.0
  14. author: Alice-Knight
  15. # logging
  16. logging:
  17. file:
  18. name: app.log
  19. path: ../logs
  20. logback:
  21. rollingpolicy:
  22. max-file-size: 5MB
  23. max-history: 15
  24. pattern:
  25. dateformat: yyyy-mm-ddTHH:MM:ss.SSSXXX
  26. # database & cache & es
  27. spring:
  28. datasource:
  29. url: jdbc:mysql://localhost:3306/demo
  30. username: root
  31. password: 123456
  32. driver-class-name: com.mysql.jdbc.Driver
  33. data:
  34. redis:
  35. database: 0
  36. connect-timeout: 120
  37. port: 6379
  38. host: 0.0.0.0
  39. jedis:
  40. pool:
  41. enabled: true
  42. max-active: 10
  43. min-idle: 2
  44. elasticsearch:
  45. uris:
  46. - http://localhost:9200
  47. username: admin
  48. password: 123456
  49. connection-timeout: 120s

3.配置读取

  1. @Value("${field}")

通过注解 @Value 解析配置中的字段,新建个控制器类,主要功能就是返回 app 的 info:

  1. package com.example.demo;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. @RestController
  8. public class RestfulController {
  9. @Value("${app.version}")
  10. private String version;
  11. @Value("${app.author}")
  12. private String author;
  13. @Value("${app.desc}")
  14. private String desc;
  15. @GetMapping(value = "/appInfo")
  16. public Object getAppInfo() {
  17. Map<String, String> info = new HashMap<>();
  18. info.put("version", version);
  19. info.put("author", author);
  20. info.put("desc", desc);
  21. return info;
  22. }
  23. }

测试:
在这里插入图片描述

从结果中可以看到,返回的响应体包含我们配置文件中的字段信息。

  1. @Component + @ConfigurationProperties + @AutoWired

这里我们用到自动装配注解。首先实现对象类,就是一个 Bean, 然后类加上注解:

  1. @Component
  2. @ConfigurationProperties(prefix = "app")

其次在使用到的控制类中设置变量,加上注解 @AutoWired,具体实现如下:
AppInfo.java

  1. package com.example.demo;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. @ConfigurationProperties(prefix = "app")
  6. public class AppInfo {
  7. private String author;
  8. private String desc;
  9. private String version;
  10. public String getAuthor() {
  11. return author;
  12. }
  13. public void setAuthor(String author) {
  14. this.author = author;
  15. }
  16. public String getDesc() {
  17. return desc;
  18. }
  19. public void setDesc(String desc) {
  20. this.desc = desc;
  21. }
  22. public String getVersion() {
  23. return version;
  24. }
  25. public void setVersion(String version) {
  26. this.version = version;
  27. }
  28. }

AppInfoController.java

  1. package com.example.demo;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. @RestController
  8. public class AppInfoController {
  9. @Autowired
  10. private AppInfo appInfo;
  11. @GetMapping(value = "/v2/appInfo")
  12. public Object getAppInfoV2() {
  13. Map<String, String> info = new HashMap<>();
  14. info.put("version", appInfo.getVersion());
  15. info.put("author", appInfo.getAuthor());
  16. info.put("desc", appInfo.getDesc());
  17. return info;
  18. }
  19. }

测试结果:
在这里插入图片描述

从结果中可以看到采用该方法也可以实现配置项的读取。

4.自定义配置

比如我们在配置文件中定义了这个 app 的name、开发者、版本号等,即如下:

  1. app:
  2. name: demo
  3. author: david-knight
  4. version: 1.0.0
  5. ...

这个自定义的内容,我们又怎么获取呢?
先写个 Bean 来接收 app 的字段:

  1. package com.example.springbootdemo2.param;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. @ConfigurationProperties(prefix = "app")
  6. public class AppInfo {
  7. private String name;
  8. private String desc;
  9. private String version;
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public String getDesc() {
  17. return desc;
  18. }
  19. public void setDesc(String desc) {
  20. this.desc = desc;
  21. }
  22. public String getVersion() {
  23. return version;
  24. }
  25. public void setVersion(String version) {
  26. this.version = version;
  27. }
  28. }

然后在 控制器类中 自动装配 Object:

  1. package com.example.springbootdemo2.controller;
  2. import com.example.springbootdemo2.param.AppInfo;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. @RestController
  10. @RequestMapping(value = "/app")
  11. public class AppInfoController {
  12. @Autowired
  13. private AppInfo appinfo;
  14. @GetMapping(value = "/info")
  15. public Object getAppInfo() {
  16. Map<String, Object> info = new HashMap<>();
  17. info.put("name", appinfo.getName());
  18. info.put("desc", appinfo.getDesc());
  19. info.put("version", appinfo.getVersion());
  20. return info;
  21. }
  22. }

上面添加注解时,提示有点小问题,根据官方建议,在 pom.xml 中添加 配置依赖:

  1. <!-- 配置处理 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-configuration-processor</artifactId>
  5. <optional>true</optional>
  6. </dependency>

看看测试效果:
在这里插入图片描述

从结果中可以看到,配置项已经成功读取到。

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读