Home > other >  Whether springboot can specify application. Yml file path for loading
Whether springboot can specify application. Yml file path for loading

Time:03-26




Springboot whether can specify application. Yml file path for loading, not under the resource

CodePudding user response:


24.3 the Application property files
SpringApplication will load the properties from the application. The properties files in the following locations and add them to the Spring Environment:

A/config subdirectory of the current directory.
The current directory
A classpath/config package
The classpath root

The list is ordered by precedence (The properties defined in locations who override in The list are those defined in The lower locations).






You can also use YAML files (. Yml ') as an alternative to 'properties'.



If you don 't like application. The properties as the configuration file name you can switch to another by specifying a spring config. Name the environment property. You can also refer to the an explicit location using the spring. The config. The location environment property (comma - separated list of directory locations, or the file paths).
$Java - jar myproject. Jar - spring. Config. Name=myproject
Or
$Java - jar myproject. Jar - spring. Config. The location=classpath:/default properties, the classpath:/override the properties






Spring. Config. The name and spring. Config. The location are 2 very early to determine which files have to be the loaded so they have to be defined as an environment or property (typically OS env, the system property or the command line argument).



If spring. Config. The location the contains directories (as opposed to files) they should end in/(and will be appended with the names generated from spring. Config. The name before being the loaded, o profile - specific file names). The files specified in the spring. The config. The location are informs the as-is, with no support for the profile - specific variants, and will be overridden by any profile - specific properties.
The Config locations are searched in reverse order. By default, the configured locations are the classpath:/, the classpath://Config file:./, file:./Config/. The resulting search order is:

File:./config/
File:/
The classpath:/config/
The classpath:/

When the custom config locations are configured, they are 2 in addition to the default locations. Custom locations are searched before the default locations. For example, if the custom locations classpath:/custom config/file:./custom config/are configured, the search order becomes:

File:./custom config/
The classpath: custom config/
File:./config/
File:/
The classpath:/config/
The classpath:/

This search ordering allows you to specify default values in the configuration file and then selectively override those values in another. You can dojo.provide default values for you application in application. The properties (or whatever other the basename you choose with spring. Config. The name) in one of the default locations. These default values can then be overriden at runtime with a company's file located in one of the custom locations.






If you use environment variables rather than the system properties, most operating systems, disallow period - separated key names, but you can use underscores home (e.g. Home SPRING_CONFIG_NAME spring. Config. The name).









If you are running in a container then the JNDI properties (in Java: comp/env) or servlet context initialization parameters can be home, informs, or, as well as environment variables or the system properties.



CodePudding user response:

In application, yml specified inside you to reference the location of the yml
Spring:
Config:
Location:../yml - test/application. Yml
Can also use an absolute path

  • Related