SpringBoot Advanced - Configuration Advanced

Official documents related to SpringBoot configuration: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

1. Temporary properties

1. Command line setting properties

Add temporary attributes when using the java -jar command to start the jar package

--属性名=属性值Use the form to specify temporary attributes, and separate multiple attributes with spaces (each attribute name must be preceded --)

Example:

java -jar example-0.0.1-SNAPSHOT.jar --server.port=8080 --logging.level.root=warn

2. IDEA sets parameters at startup

Select the program to start in Edit Configurations in the upper right corner of the idea, set Program arguments, and the content format is consistent with the parameters set on the command line

As shown below:

insert image description here
insert image description here

3. Refusal to accept temporary parameters

The Args parameter is passed in the SpringBoot project startup class, as shown in the following example:

@SpringBootApplication
public class Application {
    
    
    public static void main(String[] args) {
    
    
        args = new String[]{
    
    "--server.port=8080", "--logging.level.root=warn"};
        SpringApplication.run(Application.class, args);
    }
}

As above, args in the main function is the startup parameter passed in by the command line or idea, which can be modified by writing a program

The SpringApplication.run() method can not pass in the args parameter, so as to achieve the effect of refusing to receive temporary attributes

For example:

@SpringBootApplication
public class Application {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Application.class);
    }

}

Second and fourth level configuration files

  1. Configuration files under the development directory resources/ directory (lowest priority)
  2. Configuration files under the development directory resources/config/ directory
  3. The configuration file of the same directory as the jar package
  4. The configuration file under the config/ directory at the same level as the jar package (highest priority)

insert image description here

When configuration files with different priorities exist at the same time, different configurations take effect complementary to each other, and the same conflicting parts take effect with higher priority

At the same time, different types of configuration files also have different priorities: yaml < yml < properties

3. Specify the configuration file

1. Specify the configuration file name

(idea or command line) Pass in parameters at startup --spring.config.name=filenamewithout file extension

The default value of spring.config.name is application (so the configuration file name is application)

After specifying another file name, the application configuration file will become invalid, and only new file names will be scanned in the same four-level configuration files

Multiple names are separated by commas, and specifying later takes precedence over specifying first, for example:

java -jar example-0.0.1-SNAPSHOT.jar --spring.config.name=application,tempconfig

The application is in front of the tempconfig, and the different parts are complementary, and the conflicting part of tempconfig covers the application

2. Specify the configuration file path

(idea or command line) Pass in parameters at startup --spring.config.location=fileSpecify the path of the configuration file

Support absolute path and relative path, support classpath:/, must have a suffix name

Multiple files are separated by commas, specifying later takes precedence over specifying first

Example:

java -jar example-0.0.1-SNAPSHOT.jar --spring.config.location=classpath:/application.yml,tempconfig.yml

Note that tempconfig.yml above is a relative path to the current directory, not in classpath:/

4. Multi-environment development

1. yaml single file multiple environments

Use ---to separate different environments, use spring.config.activate.on-profile to specify the name of the current environment in different environments

The part where the environment name is not specified is the default configuration, or public configuration, where spring.profiles.active is used to specify the environment name to take effect

Examples are as follows:

# 公共配置
  profiles:
    active: dev	# 生效的环境,多个环境逗号分隔,冲突部分后声明覆盖先声明
---
# 开发环境
spring:
  config:
    activate:
      on-profile: dev,env_dev # 可指定多个环境名称,逗号分隔
server:
  port: 8080

The spring.profiles property can also be used to specify the current environment name, the effect is the same as spring.config.activate.on-profile, but outdated

---
# 过时写法
spring:
  profiles: env1
---
# 推荐写法
spring:
  config:
    activate:
      on-profile: env2

2. yaml multi-file multi-environment

Different environments can be written into different configuration files, the file name is application-env.yml, where env is the environment name

The application.yml file is the default configuration:

spring:
  profiles:
    active: dev	# 生效的环境

application-dev.yml represents the configuration of the dev environment:

server:
  port: 8081

Different environments no longer need to set the environment name, but are determined by the file name

3. properties multi-file multi-environment

Same as yaml multi-file, different environment configurations are written in different properties files named application-env.properties

The application.properties file is the default configuration:

spring.profiles.active: dev	# 生效的环境

application-dev.properties represents the configuration of the dev environment:

server.port: 8081

In addition, the configuration of the properties file cannot write multiple environments into the same properties file, and multiple environments can only be set by multiple files

4. Multi-environment groups

For multi-environment group-related configurations, it is best to write different environments in different files. Writing in the same file may cause the loading order to be incorrect.

(1) include

Example:

spring:
  profiles:
    active: dev	# 生效的环境
    include: devMysql,devRedis	# 引入其它环境到组内

In the above configuration, the dev environment will be used to simultaneously introduce the two environment configurations of devMysql and devRedis

The main environment is loaded last, and the loading order is: devMysql, devRedis, dev, after the conflict configuration is loaded, the override is loaded first

Not very practical, equivalent to active: devMysql, devRedis, dev

(2) group

Example:

spring:
  profiles:
    active: dev	# 生效的环境
    group:
      "dev": devMysql,devRedis
      "pro": proMysql,proRedis
      "test": testMysql,testRedis

Use group to configure multiple environment groups and members, and switch groups only need to change the active main environment

Note: The main environment is loaded first , and the loading order is dev, devMysql, devRedis

5. Cooperate with Maven multi-environment

Maven multi-environment configuration article guidance: https://blog.csdn.net/Cey_Tao/article/details/126602653

Look at the Maven configuration first:

<profiles>
	<profile>
		<!-- 开发环境 -->
		<id>env_dev</id>
		<properties>
			<spring.profile>dev</jdbc.url>
		</properties>
		<activation>	<!-- 默认生效 -->
			<activeByDefault>true</activeByDefault>
		</activation>
	</profile>
	<profile>
		<!-- 生产(正式)环境 -->
		<id>env_pro</id>
		<properties>
			<spring.profile>pro</jdbc.url>
		</properties>
	</profile>
 </profiles>

Look at the yaml configuration file again:

spring:
  profiles:
    active: @spring.profile@	# 读取 Maven 配置中的 spring.profile 属性

Since the spring.profile property has different values ​​in different environments of Maven

So when you specify the Maven environment for compilation or packaging, you also specify the SpringBoot environment

[Replenish]

After Maven changes the default effective environment, the idea startup project may not take effect. When the idea cache problem occurs, it needs to be manually compiled and then started to take effect.

Guess you like

Origin blog.csdn.net/Cey_Tao/article/details/127560395