Conditionnaly replace spring boot config yml property - java

I would like to replace a configuration yml property using a condition based on environnement variables :
spring:
datasource:
username:${ENV} == 'PROD' ? ${USER_PROD} : ${USER_TEST}
password: ${ENV} == 'PROD' ? ${PWD_PROD} : ${PWD_PROD}
Is there any way I can do this inside my application.yml or programmaticaly ?
I have not faced this situation before

The normal way of doing this is to use different application.properties file each representing a "profile".
Then you can override the desired properties based on the profile and run the application using that profile using -Dspring.profiles.active.
A useful guide on the following link.

you can use separate config files and properties for different Spring Profiles.
please read this guide for more info: Guide

Related

How to resolve placeholder in properties file with values from another properties file in spring boot application

My spring boot application has below properties files.
src/main/resources/config/DEV/env.properties
mail.server=dev.mail.domain
src/main/resources/config/QA/env.properties
mail.server=qa.mail.domain
src/main/resources/config/common/env.properties
mail.url=${mail.server}/endpoint
Is it possible to load "common/env.properties" so that it's placeholders will be resolved using the given environment specific properties file. For DEV environment, we want the placeholders in "common/env.properties" to be resolved using values from "DEV/env.properties".
There are answers about how to load multiple properties files and profile based loading but could not find an answer for this particular use case.
Thanks in advance.
2 Options :
Generate the common/application.properties using configuration-maven-plugin and filter files for each environment. It is outdated now.
Use application-<env>.properties for each environment and pass the -Dspring.profiles.active=<env> as VM option in application start up. Spring will automatically take the property from correct file.
In option 2, you will be overwriting whatever is present in application.properties with application-.properties. So you dont have to add only the properties which you need to change per environment.
for eg:
Your application.properties can have
logging.level.root=WARN
logging.level.org.apache=WARN
logging.level.org.springframework=WARN
Your application-dev.properties can have
logging.level.org.springframework=DEBUG
which means, when you are starting application using dev profile, spring takes
logging.level.root=WARN
logging.level.org.apache=WARN
logging.level.org.springframework=DEBUG
edit :
Also, you can try something like below on your class. (Spring will overwrite value in config.properties with values from config-dev.properties). ignoreResourceNotFound will make sure, application will still start with default values even if the corresponding file is not found.
#Configuration
#PropertySource("classpath:config.properties")
#PropertySource(value = "classpath:config-${spring.profiles.active}.properties", ignoreResourceNotFound = true)
You can add resources/application.yml file where you can have multiple profiles in one File.
MultiProfile Yaml
e.g.here are two different profiles 'dev' and 'qa' with different applicationNames 'DEV' and 'QA' and one defaultName 'Default'
spring:
application:
name: Default
profiles:
active: qa
---
spring:
profiles: dev
application:
name: DEV
---
spring:
profiles: qa
application:
name: QA
You can achieve this by declaring a property source on a class configuration and setting up an environment variable in the path :
#PropertySource({ "classpath:config/${env}/env.properties" })
#Configuration
public class config{}
And then you launch the spring boot app with the command line variable -env=dev
UPDATE
You can use #PropertySources annotation to load several properties.
#PropertySources({
#PropertySource("classpath:config/${env}/env.properties"),
#PropertySource("classpath:config/common/env.properties")
})
public class config{}

adding new property based on spring profile

If spring boot is run in override profile , can we have application-override.properties having properties like foo.baz that is not defined in application.properties ?
application.properties
foo.bar=1
application-override.properties
spring.profiles.include=default
foo.baz=1
That is correct. When you have new properties in application-override.properties and and the override profile is the active profile, then yes in your program the properties from application.properties as wel as application-override.properties is loaded.
Using spring.profiles.include=default in your override profile is not needed.
In the case of loading multiple specific profiles with same properties:
Also, in the context of property overriding with profiles, something to keep in mind when you have multiple active profiles and they contain the same property. The last profile in the list will be used.
Let's say you start up your program with mvn spring-boot:run -Drun.profiles=profile1,profile2
Both application-profile1.properties and application-profile2.properties
contains the property my.custom-property=x (for profile1) and my.custom-property=y (for profile2). The value of my.custom-property will be y, as that was the last profile in the provided profiles.
You can create configuration class for your custom profile and load the appropriate properties file in it like this:
#Configuration
#Profile("override")
#PropertySource("classpath:application-override.properties")
public class OverrideConfig {
}
This way, all the configuration you do in OverrideConfig (including taking properties from application-override.properties), will only load if override profile is enabled in application.properties like this:
spring.profiles.active=override
Long story short : Spring boot overrides values of properties with same name according to their evaluation order. But here you don't override any property, you add a new.
That is still simpler : Spring boot just adds it into the Spring Environment.
Just run the app by specifying this profile and makes sure that the properties are located in the locations expected by Spring Boot.
Example from a fat jar (Java system property) :
java -Dspring.profiles.active=override -jar foo.jar
Example from the source code (Maven property) :
mvn spring-boot:run -Dspring-boot.run.profiles=override
Yes, you can do this by simply add the profile name to the application.properties:
application-override.properties
Then you can load profile from the command line:
java -jar foo.jar --spring.profiles=override
source: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment
Spring will load the application.properties first followed by any application-{profile}.properties.
Another option is to use yaml, and load everything into one file:
foo:
bar: 1
---
spring:
profiles: override
foo:
baz: 1
---
spring:
profiles: otherOverride
foo:
bar: 2
baz: 2

How do I get multiple default values in properties across different profiles?

I have a few microservices with the same configuration, with each depending on a Spring profile. #ConfigurationProperties takes the values that I have in my YAML application profile file. I want to move this configuration to a common project and out of the microservices YAML configuration.
The problem is that I have 2 different configurations for different profiles. I can use #Value to inject default values, but I can do that only once. Is there a way to have multiple default values contingent on a specific Spring profile?
In my project we use Java configuration, not XML.
Do you mean like below ? I have two profiles A1 & B1 with the same property (app.port) in the same yml file (application.yml). The profiles are separated by ---
---
spring:
profiles: A1
app.port: 8080
---
spring:
profiles: B1
app.port: 9090
---
If you want to read properties from common location then write bootstrap.yml in your microservices and delete application.yml.
bootstrap.yml:
spring:
config:
location: file:/home/external/properties/location/
name: application
profiles:
active: dev
file location: /home/external/properties/location/
suppose you need dev and prod environment.Then keep this 3 properties file in this location.
application.yml
application-dev.yml
application-prod.yml
Spring boot read application.yml properties first. If set active profile dev in bootstrap.yml then application.yml value overwirte by application-dev.yml.
For different value just change active profile in bootstrap.yml of your microservices.
According to my search, I cannot resolve this issue as I want. Probably spring-cloud-config would resolve my problem but I don't want to implement this one. I changed the library and don't need to have these separate configs anymore.

Can we use multiple yaml files in a single spring boot application?

I have one yaml file that reads all the environment profiles. I need another yaml file to create a feature switch that i can turn on/off during deployment. And how can we define the feature switch in properties file.
Yes, you can use multiple YAML files if you use spring profile. For example, if you start your JVM with the following flag:
-Dspring.profiles.active=deployed,cassandra
It will pick up the following application YAML files:
application.yml, application-deployed.yml, and application-cassandra.yml
Another way to do this, is by adding this key in the application.yml file like below
spring:
profiles:
include:
- student
- address
and you can name your yaml files, as
application-student.yml, application-address.yml

spring yml file for specific environment

I have 3 yml files namely
application-default.yml -> default properties, should be available
in all profiles
application-dev.yml -> properties only for dev
profile
application-prod.yml -> properties only for prod profile
When I start my boot application by passing the -Dspring.profiles.active=dev,I am able to access the application-dev.yml specific properties.
But I cant get the properties defined in the application-default.yml files.
Following is my application-dev.yml file:
Spring:
profiles:
include: default
spring.profiles: dev
prop:
key:value
TL;DR
Just rename the application-default.yml file to application.yml and will work as you expect.
Explanation
According to the description in the docs, a file called application-{suffix}.yml is activated when you run your application with the profile which name matches with the suffix. In addition, the main application.yml is loaded by default so it's the perfect place to put common properties for all profiles. Alternatively, if you want to keep the name of your file as application-default.yml you can pass two profiles to your Spring Boot application:
-Dspring.profiles.active=default,dev
This way you will activate two profiles and both properties files will be loaded.
I was able to solve my problem, here is what I did.
Created a file application-common.yml, put the common properties there.
Then in the application-{env}.yml files I put this on the top.
spring:
profiles:
include: default
Since I dont need to ever load the default profile specifically, this works for me!!!
What I do is:
Put common settings in application.xml, and in this file add:
spring:
profiles:
active: dev, pro, xxx...
all the profiles you want to activate.
So that you just edit this file to switch environment.
Remember that external files procedes, so you can leave another application.xml outside of the WAR to activate dev/pro/... environment instead of editing this file every time. Be sure to check the documentation:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Categories