The JAR package reads the internal and external configuration files of the jar package, and the method for springboot to read the external configuration files (priority configuration)

The jar package reads the internal and external configuration files of the jar package, and the method for springboot to read the external configuration files 

Use the system property System.getProperty("user.dir") to get the directory where the command is executed ( many on the Internet say that the current directory of the jar package is wrong )

Example source code:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class TestConfigPath {

    public static void main(String[] args) {
        String path = System.getProperty("user.dir");
        System.out.println(path);
        Properties properties = TestConfigPath.getProperties("application.properties");
        String activemqConnection = properties.getProperty("activemq.connection");
        System.out.println(activemqConnection);
    }

    public static Properties getProperties(String fileName) {
        try {
            String outpath = System.getProperty("user.dir")+File.separator+"config"+File.separator;//先读取config目录的,没有再加载classpath的
            System.out.println(outpath);
            Properties properties = new Properties();
            InputStream in = new FileInputStream(new File(outpath + fileName));
            properties.load(in);
            return properties;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            try {
                Properties properties = new Properties();
                InputStream in = TestConfigPath.class.getClassLoader().getResourceAsStream(fileName);//默认加载classpath的
                properties.load(in);
                return properties;
            } catch (IOException es) {
                System.out.println(es.getMessage());
                return null;
            }
        }
    }
}

Note: If you run in docker and put the config directory in the same directory as the jar, you can’t read it. You need to add a sentence in the Dockerfile: ADD config/ /config/

Then the path read out is: //config/application.properties

 

/config/application.properties
/config/application.properties (No such file or directory)

 

The configuration file in the docker config has a higher priority than the jar package, and will override the default configuration in the jar package. When deploying to other server environments, you only need to modify the corresponding configuration.
Dockerfile configuration needs to add: ADD config/ /config/

 

The method for springboot to read external configuration files has the following priorities: The
first is to create a config folder under the directory where the command is executed. (Create a config folder in the same directory of the jar package, and execute commands in the jar package directory), and then put the configuration file in this folder.
The second is to directly put the configuration file in the same level directory of the jar package.
The third is to create a config folder under the classpath, and then put the configuration file into it.
The fourth is to put the configuration file directly under the classpath.
By default, springboot first reads a config/application.properties file in its own directory at the same level.
The application.properties file created under the src/main/resources folder has the lowest priority

So when springboot starts to read external configuration files, you only need to add a layer of configuration files to override the default ones without modifying the code.

 

 

 

 

Guess you like

Origin blog.csdn.net/bj_chengrong/article/details/103185706