Spring boot uses external configuration files

Kenight_ 2022-06-24 08:03:39 阅读数:990

springbootusesexternalconfiguration

External use  application.properties

SpringApplication By default, it will be loaded from the following location  application.properties file

1.A /config subdirectory of the current directory : Current directory config Catalog Next

2.The current directory : Under current directory

3.A classpath /config package : classpath The root path Of config It's a bag

4.The classpath root : classpath Under the root path

priority 1 > 2 > 3 > 4 , If all four positions exist , The higher priority will override the lower priority , It's covered with attribute And more attributes can be superimposed

Spring There are many other ways to specify a configuration file , Please refer to the official documentation  Externalized Configuration chapter


Overwrite internal with external configuration file Custom profile attribute

adopt @PropertySource annotation , And define multiple paths , The following attributes will override the previous , Such as :

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

ignoreResourceNotFound = true, Ignore the error prompt when it cannot be found , Otherwise, the development environment will not compile , Because the file under this path cannot be found

When you need to override attributes , Just in jar Put in peer directory config Folder and put  config.properties file ( Same as application.properties 1 Ways in )

copyright:author[Kenight_],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240219094881.html