The Spring boot configuration file is placed outside the jar

Source: Internet
Author: User

The Spring boot program reads the configuration from Application.properties or Application.yaml by default, so how can I configure the configuration information externally and conveniently?

Check the official website, you can get the following scenarios:

Specify by command line

Springapplication will convert command-line option parameters to configuration information by default
For example, the command parameter at startup specifies:

java -jar myproject.jar --server.port = 9000

Specifying configuration items from the command line has the highest priority, but you can disable them by setaddcommandlineproperties

SpringApplication.setAddCommandLineProperties(false).
External configuration file

The spring program loads the application.properties configuration file from the following paths in priority order

    • /config directory under the current directory
    • Current directory
    • The/config catalogue in Classpath
    • Classpath and Directory

Therefore, it is very simple to have an external configuration file, create a new config folder in the directory where the Jar is located, and then put the configuration file, or directly in the configuration file in the Jar directory

Custom configuration Files

What if you don't want to use application.properties as a configuration file? No problem at all.

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

Or

java -jar -Dspring.config.location=D:\config\config.properties springbootrestdemo-0.0.1-SNAPSHOT.jar 

Of course, you can also specify in the code

@SpringBootApplication@PropertySource (value={"File:config.properties"})public class springbootrestdemoapplication { public static void  Main(string[] args) {springapplication.  Run (springbootrestdemoapplication.  class, args); }}
Read different configurations by profile different environments

Configurations for different environments set up a configuration file, for example:

    • Configuration configuration in the dev environment is in application-dev.properties;
    • Configuration in the PROD environment is configured in Application-prod.properties.

Specify which file to use in Application.properties

spring.profiles.active = dev

Of course, you can also specify it manually at run time:

java -jar myproject.jar --spring.profiles.active = prod

Reference:
1 See also externalized Configuration

Jadepeng
Source: Jqpeng's technical notepad--http://www.cnblogs.com/xiaoqi
Your support is the greatest encouragement to bloggers, thank you for your serious reading.
The copyright of this article is owned by the author, welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

The Spring boot configuration file is placed outside the jar

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.