(Transfer) SpringBoot is packaged as a war package to import external configuration files at startup

Reprinted from: http://blog.csdn.net/nijiayy/article/details/78457800

 

Recently, I was working on a SpirngBoot project. When the server was deployed, it was required to import an application.properties file in a specified location when using tomcat to start the war package. After looking up related issues on the Internet, I found that most of them are based on jar packages, and the same method does not apply to war packages. 
Later found a way to solve this problem perfectly. 
write picture description here 
In your project folder, create a configuration folder to store various SpringBoot configuration files, and then create a new Java class LocalSettingsEnvironmentPostProcessor.

package com.altynai.xxxxxx.configuration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

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

public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor{
    private static final String LOCATION = "C:\\xxxx\\application.properties";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
        File file = new File(LOCATION);
//        File file = new File(System.getProperty("user.home"), LOCATION);
//        System.out.println("user.home" + System.getProperty("user.home"));
        if (file.exists()) {
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
//            System.out.println("Loading local settings from " + file.getAbsolutePath());
            Properties properties = loadProperties(file);
//            System.out.println(properties.toString());
            propertySources.addFirst(new PropertiesPropertySource("Config", properties));
        }
    }

    private Properties loadProperties(File f) {
        FileSystemResource resource = new FileSystemResource(f);
        try {
            return PropertiesLoaderUtils.loadProperties(resource);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
        }
    }
}

The role of this java class is to read the file according to the specified configuration file path and add it to the environment where the program runs. Note that according to my tests, the external configuration file imported here can only be a .properties file, and there will be problems if it is .yml. 
Then create a folder named META-INF in your resources folder, and create a spring.factories file in it. The contents of the file are as follows:

org.springframework.boot.env.EnvironmentPostProcessor=com.altynai.xxxxx.configuration.LocalSettingsEnvironmentPostProcessor

The purpose of this file is to call the Java class we just wrote when the SpringBoot service starts.

At this point, your war package should be able to read the external files in the specified location when it is started with tomcat. My file structure is as follows, just for reference 
write picture description here 
. What needs to be explained here is that it is assumed that the setting properties of your original configuration file and the setting properties of the imported external configuration file are duplicated. Which one is used when the final system runs? 
The answer is, look at the order in which the two configuration files are added to the program environment, and the latter will overwrite the former. 
There is this piece of code in the Java class above, which means that the external file is imported first.

 propertySources.addFirst(new PropertiesPropertySource("Config", properties));

If set to this below

 propertySources.addLast(new PropertiesPropertySource("Config", properties));

It means that the external configuration file is imported last.

Reference link: 
[1]  https://www.youtube.com/watch?v=uof5h-j0IeE&feature=youtu.be&t=1h17m46s 
[2]  https://www.youtube.com/watch?v=uof5h-j0IeE&feature= youtu.be&t=1h17m46s

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326224725&siteId=291194637