Section 8: specifying the configuration file for SpringBoot

Rules for spring boot configuration properties

  • Pass Separate elements
  • the last one. Separate prefix from attribute name
  • Must be letters (az) and numbers (0-9)
  • Must be lowercase
  • Separate words with hyphens -
  • The only other characters allowed are [and], which are used for the index of the List
  • Cannot start with a number

Same configuration item

Spring Boot 2.x when loading the configuration file, special characters will be removed, and the configuration will be matched and loaded in all lowercase.

application.properties

com.rumenz.id-name=rumenz
com.rumenz.id_name=rumenz
com.rumenz.idName=rumenz
com.rumenz.idname=rumenz

test

package com.rumenz.lession8.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @className: RumenzController
 * @description: TODO Class description
 * @author: Entry station rumenz com
 * @date: 2021/11/8
 **/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {


    @Value("${com.rumenz.idname}")
    private String comRumenzIdName;

    @RequestMapping("/index")
    @ResponseBody
    public String index(){
        return comRumenzIdName;
    }
}

Browser access http://127.0.0.1:8080/rumenz/index , return rumenz

The above four configuration items are equivalent. It is recommended to use - division in the middle of all lowercase.

Priority of profile

application.properties and application YML files can be placed in the following locations.

  • External: config subdirectory of the application running directory
  • External: the following directory of the application running directory
  • Internal: classpath:/config in config package/
  • Internal: in the classpath root directory classpath:/
routeParameter value
/springboot-zhinan/lession8/config/application.propertiescom.rumenz.level=1
/springboot-zhinan/lession8/application.propertiescom.rumenz.level=2
/springboot-zhinan/lession8/src/main/resources/config/application.propertiescom.rumenz.level=3
/springboot-zhinan/lession8/src/main/resources/application.propertiescom.rumenz.level=4

application. Com.com is configured in properties rumenz. level

com.rumenz.level=1

test

package com.rumenz.lession8.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @className: RumenzController
 * @description: TODO Class description
 * @author: Entry station rumenz com
 * @date: 2021/11/8
 **/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {
    
    @Value("${com.rumenz.idname}")
    private String comRumenzIdName;

    @Value("${com.rumenz.level}")
    private String comRumenzLevel;

    @RequestMapping("/index")
    @ResponseBody
    public String index(){
        return comRumenzIdName;
    }

    @RequestMapping("/index1")
    @ResponseBody
    public String index1(){
        return comRumenzLevel;
    }

}

After packaging with mvn clean package, use Jar - jar lesion8-0.0.1-snapshot Jar running. If application is defined in the above four places Properties, and com.com is configured rumenz. Level, when our business needs to use COM rumenz. Level configuration item. The priority of search is:

  • 1. First find the application of config in the running directory Com.com in properties rumenz. Level, find and return, otherwise go to the next step
  • 2. Find the application under the running directory Com.com in properties rumenz. Level, find and return, otherwise go to the next step
  • 3. Find the application in the config directory under classpath Properties rumenz. Level, find and return, otherwise go to the next step
  • 4. Find the application under classpath Properties rumenz. Level, return if found, report an error if not found, and the project fails to start.

Command line parameter configuration

springboot application Some parameters can be configured in properties, such as client slogan, account number and password. If we want to temporarily modify the running port during operation, it is also possible.

java -jar lession8-0.0.1-SNAPSHOT.jar --server.port=9000

In this way, the running port of the project becomes 9000, -- server Port = 9000 is equivalent to application Configure server in properties port=9000

System environment variable

We can also define variables in the operating system and inject them into the Spring container through @ Value.

package com.rumenz.lession8.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @className: RumenzController
 * @description: TODO Class description
 * @author: Entry station rumenz com
 * @date: 2021/11/8
 **/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {

    //Read system environment variables
    @Value("${PATH}")
    private String path;
    
    @RequestMapping("/index2")
    @ResponseBody
    public String index2(){
        return path;
    }
    
}

Browser access http://127.0.0.1:8080/rumenz/index2 , return / usr / local / bin: / usr / bin: / bin: / usr / SBIN: / SBIN: / usr / local / go / bin: / usr / local / share / dotnet: ~ / dotnet/tools:/usr/local/mysql/bin:/usr/local/mysql/bin

System properties

We can set VM arguments to pass parameter values to the Spring container.

Set VM arguments in Idea

test

package com.rumenz.lession8.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @className: RumenzController
 * @description: TODO Class description
 * @author: Entry station rumenz com
 * @date: 2021/11/8
 **/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {


    @Value("${spring.test.env}")
    private String springTestEnv;

    @RequestMapping("/index3")
    @ResponseBody
    public String index3(){
        return springTestEnv;
    }

}

Browser access http://127.0.0.1:8080/rumenz/index3 , return test

Run the jar package and set VM arguments

java -jar -Dspring.test.env=testvm lession8-0.0.1-SNAPSHOT.jar

Browser access http://127.0.0.1:8080/rumenz/index3 , return testvm

Source code address of this summary:

  • Follow the [entry station] and reply to [1001] to get the quick reference manual of common linux commands
  • Follow the [entry station] reply [1003] to get the solution of LeetCode [java language implementation]
  • Pay attention to [entry station] and reply to [1004] to get the summary of Java basic core
  • Follow [entry site] and reply to [1009] to obtain Alibaba Java development manual

Keywords: Spring Boot

Added by kate_rose on Thu, 10 Feb 2022 04:13:02 +0200