Springboot Packaging for a war package when you start importing external configuration files

Source: Internet
Author: User
Tags requires tomcat

Recently doing a spirngboot project that requires a server deployment to use Tomcat to launch the war package requires importing a application.properties file at the specified location. After finding the relevant issues online, most of them are based on the jar package, and the same method is not applicable under the war package. The
later found a way to solve the problem perfectly.

under your project folder, create a configuration folder to store the various springboot profiles, and 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 LO

    cation = "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.G
        Etabsolutepath (), ex); }
    }
}

The purpose 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 is running. Note that, according to my tests, the external configuration file imported here can only be a. properties file, and if it is. Yml then there will be a problem.
Then under your Resources folder create a folder named Meta-inf, in which a spring.factories file is created, 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 set the Java class that we just wrote when the Springboot service starts.

At this point, your war package should be able to read the location's external files when you start using Tomcat. My file structure is as follows, for reference only

It is also necessary to note that the settings properties of your original configuration file and the settings properties of the imported external configuration file repeat what the final system is used to run.
The answer is to look at the sequence of two configuration files that are added to the program environment, and the following will overwrite the previous one.
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 this is set to the following

Propertysources.addlast (New Propertiespropertysource ("Config", properties));

Indicates that the external configuration file was last imported.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.