Spring Boot - Configuration Introduction

Spring Boot provides a series of automated configurations for common development scenarios to reduce the original complex and rarely changed template configuration content. However, we still need to understand how to modify these automated configurations in Spring Boot to deal with some special scenarios.

configuration file

The default configuration file location of Spring Boot is src/main/resources/application.properties. The content of the configuration file about the Spring Boot application can be in this file. According to the different modules we introduce, the container port number and database connection can be defined here. Information, log level and other configuration information, commonly used configurations are as follows:

  • server.context-path: Specify the context path. For example, the default access path is http://localhost:8080. If I change it to http://localhost:8081/hello, I need to configure this property as /hello
  • server.port=8090 : Specify the service port number as 8090
  • spring.application.name=hello: Specify the application name (this name will be registered as the service name in Spring Cloud later)

custom parameters

In addition to setting the predefined configuration properties in each module in the Spring Boot configuration file, you can also define some custom properties we need in the configuration file, such as adding the following content to the configuration file:

book.name = springCloud

book.author=Li Si

Then, in the application, these custom parameters can be loaded through the @Value annotation, for example:

@Component

public class Book {

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

        private String name;

        @Value ("${book.author}")

        private String author;

}

The @Value annotation can support two expressions for configuration, as follows:

  • One is the PlaceHolder method shown in the above code, the format is ${…} and the braces are PlaceHolder
  • The other is SpEL expression (Spring Expression Language), the format is #{…}, and the braces are SpEL expressions

By custom injecting the entity, you can read the configuration properties. The sample code is as follows:

@RestController

@RequestMapping ("/hello")

public class HelloController {

   

        @Autowired

        private Book book;

   

        @RequestMapping ("getCount")

        public String getCount() {

                return "ok " + book.getName();

        }

}

parameter reference

The parameters in application.properties can be directly referenced by using PlaceHolder, for example:

book.name=springCloud

book.author=Li Si

book.desc=${book.name} by author ${book.author}

use random numbers

In some special cases, we hope that some parameters are not a fixed value each time they are loaded, such as keys, service ports, etc. In Spring Boot's property configuration file, you can use ${random} configuration to generate random int value, long value or string string, so that we can easily generate random properties by configuration, such as:

#-Randomly generate random numbers within 8080 - 8090

server.port=${random.int(8080,8090)}

#--------------------------

# - Randomly generate strings

org.drsoft.random=${random.value}

# - Randomly generate int

org.drsoft.random.int=${random.int}

# - Randomly generate long

org.drsoft.random.long=${random.long}

#-Randomly generate int within 10

org.drsoft.random.intRagne=${random.int(10)}

#-Machine generated uuid

command line arguments

Use the command java -jar to start. In addition to starting the application, this command can also specify the parameters of the application on the command line, such as java - jar xxx.jar --server.port=8090 Set the service directly on the command line Port property, when starting the Spring Boot application from the command line, two consecutive minus signs are the identifiers for assigning the properties in application.properties

多环境配置

多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,在通过打包命令指定需要打包的内容之后进行区分打包,在Spring Boot 中,多环境配置的文件名需要满足application-{profile}.properties 的格式,其中{profile}对应环境标识,如下:

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

至于具体哪个配置文件会被加载,需要在 application.properties 文件中通过spring.profiles.action 属性来设置,其值对应配置文件中的{profile} 值。

加载顺序

为了能够更合理的重写各属性的值,其Spring Boot 使用了下面这种较为特别的属性加载顺序:

  • 命令行中传入的参数
  • SPRING_APPLICATION_JSON中的属性,SPRING_APPLICATION_JSON 是以JSON格式配置在系统环境变量中的内容
  • java:comp/env 中的 JNDI 属性
  • java 的系统属性,可以通过System.getProperties()获取的内容
  • 操作系统的环境变量
  • 通过 random.* 配置的随机属性
  • 位于当前应用 Jar 包之外,针对不同{profile}环境的配置文件内容
  • 位于当前应用 Jar 包之内,针对不同{profile}环境的配置文件内容
  • 位于当前应用 Jar 包之外的 application.properties 配置内容
  • 位于当前应用 Jar 包之内的 application.properties 配置内容
  • @Configuration 注解修改的类,通过 @PropertySource 注解定义的属性
  • 应用默认属性,使用 SpringApplication.setDefaultProperties 定义的内容

可以看到其中标黄的都是从应用Jar包之外读取配置文件,所以,实现外部化配置的原理就是从此切入,为其指定外部配置文件的加载位置来取代Jar包之内的配置内容。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325698704&siteId=291194637