springboot: I want to learn the framework well, do you understand the attribute configuration and use?

springboot: I want to learn the framework well, do you understand the attribute configuration and use?

After introducing the springboot framework yesterday, everyone's enthusiasm for this framework is still good. So today Damon will continue to introduce more in-depth things about springboot. Hope to help everyone grow and progress. If you want to learn about yesterday's article, you can go to github or read the previous records to see it.
Spring Boot property configuration and use
  • Simply put, Spring Boot allows you to use the code of the same application in different environments through external configuration, perhaps it is possible to inject properties or modify the default configuration through configuration files.
Spring Boot supports multiple external configuration methods
The priority of these methods is as follows:
    1. Command line parameters
    1. JNDI attributes from java:comp/env
    1. Java system properties (System.getProperties())
    1. Operating system environment variables
    1. Random.* property value configured by RandomValuePropertySource
    1. Application-{profile}.properties or application.yml (with spring.profile) configuration file outside the jar package
    1. Application-{profile}.properties or application.yml (with spring.profile) configuration file inside the jar package
    1. Application.properties or application.yml (without spring.profile) configuration file outside the jar package
    1. The application.properties or application.yml (without spring.profile) configuration file inside the jar package
    1. @PropertySource on @Configuration annotated class
    1. The default properties specified by SpringApplication.setDefaultProperties
Command line parameters
  • Pass the parameters through java -jar app.jar --name="Spring" --server.port=9090.

  • The parameters are passed in the form of --xxx=xxx.

  • The parameters that can be used can be defined by ourselves or the default parameters in Spring Boot. -Many people may be concerned about issues such as how to configure the web port. These are the parameters provided in Spring Boot. Some of the available parameters are as follows:

      	# LOGGING
      	logging.config= # location of config file (default classpath:logback.xml for logback)
      	logging.level.*= # levels for loggers, e.g. "logging.level.org.springframework=DEBUG" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)
      	server.address= # bind to a specific NIC
      	server.session-timeout= # session timeout in seconds
      	server.context-parameters.*= # Servlet context init parameters, e.g. server.context-parameters.a=alpha
      	server.context-path= # the context path, defaults to '/'
      	server.servlet-path= # the servlet path, defaults to '/'
  • Note: The command line parameters are at the back of app.jar!
  • The command line configuration can be disabled by SpringApplication.setAddCommandLineProperties(false).

Operating system environment variables

  • Those who have configured JAVA_HOME should know this one.
  • Note here that some OSs may not support the use. This kind of name, such as server.port, can be configured using SERVER_PORT in this case.

Application configuration file (.properties or .yml)

  • Write directly in the configuration file: name=Isea533 server.port=8080

  • The configuration file in .yml format is such as: name: Isea533 server: port: 8080

  • When there is a prefix, it is easier to use a configuration file in the .yml format. For the usage of the .yml configuration file, please see here

  • Note: When using .yml, there must be a space between the value of the attribute name and the colon. For example, name: Isea533 is correct, and name: Isea533 is wrong.

The location of the property configuration file
  • Spring will look for application.properties or application.yml from the/config directory under the classpath or the root directory of the classpath.
  • /config takes precedence over the root of the classpath


  • This annotation can specify a specific attribute configuration file, and the priority is relatively low.


  • E.g:

      SpringApplication application = new SpringApplication(Application.class);
      Map<String, Object> defaultMap = new HashMap<String, Object>();
      defaultMap.put("name", "Isea-Blog");
Application (use) attributes

@Value( ${xxx} )

  • This method is the simplest, and the attribute value can be injected through the @Value annotation.
  • Spring Boot can easily inject properties into a configuration object. E.g:

  • Corresponding object:

      public class Config {
          private String name;
          private Integer port;
          private List<String> servers = new ArrayList<String>();
          public String geName(){
              return this.name;
          public Integer gePort(){
              return this.port;
          public List<String> getServers() {
              return this.servers;
  • Spring Boot will automatically inject properties with prefix="my" as my.

  • Spring Boot will automatically convert the type. When using List, you need to pay attention to the initialization of List in the configuration!

  • Spring Boot also supports nested attribute injection, for example: name=isea533 jdbc.username=root jdbc.password=root ...

  • Corresponding configuration class:

  •   @ConfigurationProperties
      public class Config {
      private String name;
      private Jdbc jdbc;
      class Jdbc {
          private String username;
          private String password;
      public Integer gePort(){
          return this.port;
      public Jdbc getJdbc() {
          return this.jdbc;


  • The attributes at the beginning of jdbc will be injected into the Jdbc object.

Use @ConfigurationProperties on @Bean method

  • For example: @ConfigurationProperties(prefix = "foo") @Bean public FooComponent fooComponent() {...}
  • Spring Boot will inject the attributes starting with foo into the FooComponent object according to the name matching.
  • This method is relatively rare. We will demonstrate in detail in the chapter integrating mybatis with Druid's configuration.

Attribute placeholder

  • For example: app.name=MyApp app.description=${app.name} is a Spring Boot application

  • You can refer to the previously configured attributes in the configuration file (the priority configured earlier can be used here).

  • You can also set the default value through methods such as ${app.name:default name}. When the referenced property cannot be found, the default property will be used.

  • Because the ${} method will be processed by Maven. If your pom inherits the spring-boot-starter-parent,

  • Spring Boot has changed the default ${} mode of maven-resources-plugins to @ @ mode, such as @name@.

  • If you are introducing Spring Boot, you can modify to use other separators

The "command line" parameters can also be shortened through attribute placeholders

  • For example, to modify the web default port, you need to use the --server.port=9090 method, if you write it in the configuration: server.port=${port:8080}
  • Then you can use the shorter --port=9090, and use the default value of 8080 when this parameter is not provided.

Attribute name matching rules

  • For example, there are the following configuration objects:

      public class ConnectionSettings {
          private String firstName;
  • The attribute names that can be used for firstName are as follows:
  • person.firstName, the standard camel case naming
  • person.first-name, dashed line (-) split method, recommended to use in .properties and .yml configuration files
  • PERSON_FIRST_NAME, capitalized and underlined form, it is recommended to use it in the system environment variables

Attribute validation

  • You can use JSR-303 annotations for verification, for example:

      public class ConnectionSettings {
          private InetAddress remoteAddress;
         //... getters and setters
The above is the content of Spring Boot property configuration and use. If there are some incomplete places or readers have more questions, you can check the Spring Boot complete documentation or Externalized Configuration.


As a new programmer, Damon hopes to make progress with everyone. The articles or descriptions are inadequate, I hope you can bring them up and make progress together.

Damon will continue to discover some useful consultations, knowledge and new tools, and share them with you, thank you!

Past articles have been uploaded to github, interested friends can download Star: github.com/xxxyyh/Fron...