简体   繁体   中英

Sping Boot - Separate config file other than application.yaml

I have a spring boot maven java application. I have the application.yml file and the properties specified here are used by a jar dependency of the project and I want to keep this file for them dependencies alone.

There is some additional config I want to use in the application. Is there a way to specify a second configuration file to be used within the project?

Thanks

You can specify a different name using the property spring.config.name .

From the docs :

If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property.

Create property file as example x.properties and use @PropertySourse(“classpath:x.propeties”) When need to use it

You can use @PropertySource({"classpath:first.properties", "classpath:second.properties"}) like given below,

@Component
@PropertySource("classpath:config.properties")
public class MySeparateProperties
{
 @Value( "${property.path.name}" )
 private String prop;

}

Now use this bean to use the properties where ever you want in your application

The only issue is that, if the property path is same for application.yml and config.properties application.yml will have higher priority

Edit 1

If the file is located at resources/config/config.properties then you have to give classpath:config/config.properties

Configuration files can be set by spring properties, you can use:

  • spring.config.name to select the names of the configuration files (separated by comma)
  • spring.config.location to set the paths where the configuration files are located (separted by comma)

Try to look at springboot reference doc here for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM