After the SpringBoot project is packaged into jar, how to load the external configuration file?

guide

Sometimes, some of our configuration information needs to be modified more frequently. If the configuration information is placed in the project, then it needs to be packaged and deployed frequently, so we think whether it is possible to externalize this configuration file?

One, application.properties external

Most of the configuration information is configured in application.properties, so can this file be externalized? This is of course possible.

First define a property in application.preperties:

demo.name = hello.01

Use in Controller:

@Value("${demo.name}")
private String demoName;

@RequestMapping("/test")
public String test(){
    return this.demoName;
}

Package the project into a jar package and start it with java -jar:

java -jar springboot-out-properties-0.0.1-SNAPSHOT.jar

The value read at this time is: hello.01.

Copy the application.properties in the project, put it in the same path as the jar package, and modify the property value to:

demo.name = hello.02

Then use the above command to restart, and see the effect. The value read is hello.02. Surprised or not, Spring Boot is too awesome, and the jar package is directly read under the same path.

If we create a new config under the jar, and then put application.properties into it, can it be recognized by using the above command? The answer is yes,

SpringApplication will load properties from the application.properties file and add them to the Spring environment:

  • The /config subdirectory under the current directory
  • classpath root directory

  • The /config directory in the classpath

  • Current directory

If you customize the directory, such as conf, it will not be recognized at this time, but you can use --spring.config.location to specify the path, and execute the command as follows:

java -jar springboot-out-properties-0.0.1-SNAPSHOT.jar--spring.config.location=conf/application.properties

Of course, you can also use an absolute path to specify:

java -jar springboot-out-properties-0.0.1-SNAPSHOT.jar--spring.config.location=/Users/linxiangxian/Downloads/conf/application.properties

For more content, please pay attention to the official account [Programmer Style] to get more exciting content!

Two, @PropertySource external

In the project, some configurations will customize propreties files for use, such as defining demo.properties:

demo.nickname = hello.10
demo.weixin = springboot

Use @PropertySource to specify the configuration file:


/**
 * @PropertySource的例子
 * <p>
 */
@Configuration
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = {"classpath:demo.properties"})
public class DemoProperties {
    private String nickname;
    private String weixin;

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getWeixin() {
        return weixin;
    }

    public void setWeixin(String weixin) {
        this.weixin = weixin;
    }

    @Override
    public String toString() {
        return "DemoProperties{" +
                "nickname='" + nickname + '\'' +
                ", weixin='" + weixin + '\'' +
                '}';
    }
}

Then you can access this configuration file at this time, pack it into a jar package, and execute the command:

java -jar springboot-out-properties-0.0.1-SNAPSHOT.jar

The value returned at this time is: hello.10

Put demo.properties in the same path as the jar package, modify the value of demo.name to hello.11, execute the above command, Barbie Q, and the result is still hello.10, indicating that Spring Boot cannot handle custom properties files Look for it from the outside.

So how can we solve this problem?

Very simple, @PropertySource supports multi-configuration of multiple paths, which can be configured like this:

@PropertySource(value = {"classpath:demo.properties","file:./demo.properties"},ignoreResourceNotFound = true)

When we configure multiple paths and the configuration files exist under multiple paths, SpringBoot will load them and overwrite the same content. So when our configuration information only distinguishes external and internal paths, and the content is exactly the same, it is enough to write the file path behind. When we start locally, the classpath will be loaded because there is no file path; when the jar starts, the file path will overwrite the content under the classpath path;

ignoreResourceNotFound = true must be added, otherwise an error will be reported if it cannot be found. After adding, configuration files that cannot be found will be ignored.

At this point, just put the configuration file demo.properties at the same level as the jar package.

Summarize

For the application.properties file:

(1) The default is to read the application.properties file under the classpath.

(2) The application.properties under the same level of the jar package can be read directly, and the startup naming does not need to be adjusted.

(3) The config/application.properties under the same level of the jar package can be read directly, and the startup command does not need to be adjusted.

(4) conf/application.properties under the same level of the jar package cannot be read directly, and needs to be specified through the parameter --spring.config.location.

For custom .properties files:

(1) The default is to read the xxx.properties file under the classpath.

(2) The xxx.properties under the same level of the jar package cannot be read directly. It is necessary to modify the configuration of the code @PropertySource to specify multiple paths, and put the path that is expected to be used at the end, because it will overwrite the previously read configuration information.

Spring Boot will load properties from the application.properties file and add them to the Spring environment:

  • The /config subdirectory under the current directory
  • classpath root directory

  • The /config directory in the classpath

  • Current directory

Guess you like

Origin blog.csdn.net/dreaming317/article/details/129780419