Knowledge changes fate, stroke code makes me happy, 2020 continue to walk in the open source community click “like” to see, form a habit to give me a Star, click to understand the componentialized interface services based on SpringBoot landing solution

concept

Placeholders are a flexible configuration method that allows flexible use of configuration parameters. The @value annotation is also a representation of placeholders, which can be obtained from the Environment.

Recommended reading

  • Springboot2.x tutorial summary

Configuration mode

In application, yml/properties within the configuration file can be directly use placeholder to configure reference each other, as shown below:

system:
  name: ${spring.application.name}
spring:
  application:
    name: project-sample
Copy the code

In the above configuration, the name configuration refers directly to the spring.application.name configuration Value, so that when we use it in the system via @value (“${name}”) or @configurationProperties, The resulting values are all project-sample.

/ / @ the Value
@Value("${system.name}")
private String name;

/ / @ ConfigurationProperties way
@Configuration
@ConfigurationProperties(prefix = "system")
static class LoadConfig {
  private String name;

  public String getName(a) {
    return name;
  }

  public void setName(String name) {
    this.name = name; }}Copy the code

This greatly reduces the need for the same configuration and allows us to implement constant-like definitions in configuration files.

Use default values

When we use the @Value annotation to inject configuration parameters, if the introduced configuration is NULL, the project will be thrown an exception and will not start properly, so we need to add a default Value like this:

system:
  name: ${spring.application.name:default}
#spring:
# application:
# name: project-sample
Copy the code

${spring.application.name} {${spring.application.name} {${XXX :defaultValue}} When the configuration referenced by the placeholder is NULL, the default value is used (the default value is of a type that matches the configuration).

${system.name:default} ${system.name:default} ${system.name:default} ${system.

For configuration injection, it is recommended to use @ConfigurationProperties, which is completely OOP designed to assign values at application startup, even if the referenced configuration is NULL with no default value.

Short command line arguments

If you’re not familiar with command-line arguments, visit SpringBoot2.x Basics: Learning With Externalized Configuration Information flexibly.

Command line parameters are a good way to deploy applications where a lot of the configuration is dynamic, but SpringBoot provides long configuration parameter names that can be customized using placeholders.

Placeholders read configuration values from the Environment, and command line parameters are added to the Environment when the application starts, thus implementing dynamic placeholder configuration. This “short” means that the new configuration name you define is short.

Assume that the port number needs to be dynamically specified, you can configure the port number in the configuration file as follows:

server:
  port: ${port:8080}
Copy the code

Port is a “short” placeholder that we define, using the default 8080 if not specified at application startup.

java -jar project-sample.jar --port=9090
Copy the code

With the –port=9090 command line argument, the application starts with the port number changed to 9090.

Author’s personal blog uses the open source framework ApiBoot to help you become an Api service architect