Spring Boot yml file read order - spring

I have below files under resources folder in a standard Spring Boot app .
Spring.active.profile is set to dev
In which order the properties files are read .?
1)application.yml
2)bootstrap.yml
3)application_dev.yml
4)bootstrap_dev.yml

As Spring doc mentions
Profile specific properties are loaded from the same locations as
standard application.properties, with profiles specific files
overriding the default ones
This would mean that first the application.yml is read and then the application_dev.yml is read and overrides values from the default application.yml if needed.
Same for bootstrap.yml and bootstrap-dev.yml
Also as you can see here
bootstrap.yml is loaded before application.yml.
So to answer your question the order should be
bootstrap.yml
bootstrap_dev.yml
application.yml
application_dev.yml

bootstrap files are always the first: bootstrap.yml then bootstrap-{profile}.yml. then application.yml and application-{profile}.yml.
the property values are overridden by the next files so:
a: 1 from application.yml will be overridden by a: 55 from application-{profile}.yml

Related

#Configuration bean to set spring.application.name

What would be the appropriate way to set spring.application.name in an #Configuration file instead of within an applicaiton.properties file?
Thanks,
Brian
Basically I want to set the application name the same as the
following: myartifiact-myversion myartifact
myversion-SNAPSHOT
From docs , you can use #..# placeholders to refer to properties in pom.xml if you use spring-boot-starter-parent (Normally most spring-boot project already use it) .
So in the application.properties:
spring.application.name=#artifactId#-#version#

How to parameterize the search-locations path to be reusable in other config files

I have different files (yml & xml files) where I'm hardcoding the same folder path containing my configuration files. I want to parameterize this path, so I can be able to move my configuration and modifying my parameter only once.
My bootstrap.yml :
...
cloud:
config:
failFast: true
server:
bootstrap: true
prefix: /config
native:
search-locations: file:///C:/dev/workspace/application/config/{profile}
My application-dev.yml :
...
logging:
config: file:///C:/dev/workspace/application/config/{profile}/log4j2-dev.xml
My integration-config.xml :
<context:property-placeholder location="file:///C:/dev/workspace/application/config/${spring.profiles.active}/application-${spring.profiles.active}.properties" />
How can I achieve that ? What's the best practice in this case ? Or is it even good practice to parameterize the search-locations path for the Spring Config Server File System ?
First say I'm almost not use configuration file (yml properties xml...) directly in local-file-system(outside classpath) even for local test. And for spring-cloud-config what if you using remote location to contains config, question may different.
Second, did you using some project manager tool like maven or gradle? as maven is the default tool for spring-boot projects' build in my mind. And for maven there has plugin like Maven Resources Plugin to parameterize your config variable to pom.xml which can help you modifying parameter only once for all variable in one file in build step, and same as gradle.
For spring-cloud-config, it is possible to move your almost all configuration properties to it. Means that you can move your config properties from application-dev.yml and integration-config.xml to spring-cloud-config's config file which in your case is file:///C:/dev/workspace/application/config/{profile} and it will load as env variables in runtime for spring autoconfigure or which you can #Autowired to your Environment what if you want get it manually.

How to add profile specific properties files when I have custom name to my property file

I have properties file with name : transactionexpiry.properties in my project's src/main/resources folder.
I am able to read the properties in the code with #PropertySource("classpath:/transactionexpiry.properties")
Now I wan't to add application scope and add environment specific config files as transactionexpiry-dev.properties, transactionexpiry-local.properties, etc
But the same works with application.properties, application-dev.properties, application-local.properties
Is there a way to make it work with my previous set-up?
If you are using spring profiles:
-Dspring.profiles.active=dev
Then you can call the properties file like:
#PropertySource("classpath:/transactionexpiry${spring.profiles.active}.properties")

Is there a way to include properties file in another properties file?

I'd like to avoid cluttering the application.properties file with lots of things than, in my opinion, would be better in a separate file.
application.properties should be something like
#include module1.properties
#include module1.properties
...
###################################
######### Spring Misc #############
###################################
# Direct log to a log file
logging.file=/tmp/kmp-manager.log
#local listening port
server.port=8082
spring.profiles=nr_dev nr_testing production
spring.profiles.active=production
spring.datasource.platform=postgresql
java.security.egd=file:/dev/./urandom
Is this at all possibile?
If not, what would be a sane way to avoid cluttering?
Spring Boot Spring Boot 2.4 has added a feature for importing
We can now use spring.config.import=developer.properties to import other file. Check this blog post for more details
It's possible in YML file and the configuration are very simple
EXAMPLE:
To include properties of application-DATABASE.yml file in application.yml, use
spring:
profiles:
include: DATABASE
[EDIT - for 2.4.0 and above]
spring.profiles.group.prod=DATABASE
OR
Add the file name in application.properties
spring.config.import=classpath:application-DEV.yml,classpath:application-UDEV.yml,classpath:application-PRO.yml,classpath:application-SBA.yml
spring.config.import: file:${CLOUDJAVA_ROOT}/config/application.yaml
Imports are processed as they are discovered, and are treated as additional documents inserted immediately below the one that declares the import. Values from the imported file will take precedence over the file that triggered the import.
See spring-boot docs: Importing Additional Data

How can I suppress the default config location of Spring boot

How can I have Spring Boot only look for config files under the the directories specified by spring.config.location property and not look under the default location as specified in the ConfigFileApplicationListener javadoc.
Setting spring.config.location, causes the ConfigFileApplicationListener to look in both spring.config.location directories and the default locations.
you can use profiles.
in your application.properties you will only have :
spring.profiles.active=other
and have a file in the same folder named application-other.properties where you define your properties.
the default properties can be in a file named application-default.properties and when you want to use it, juste change the value in application.properties to 'default'.
Spring-boot documentation of Profiles

Resources