in my project i have starter and spring-boot application with this starter.
Starter contains application.yaml file with few properties.
For example:
my:
test:
value: test
spring:
profiles:
active: development
i'm adding application.yaml file to my spring-boot app, and values from starter's yaml aren't injected to context.
My question,
how i can configure 2 yaml files in starter and application with option for override values with spring-boot app's starter, for example:
spring:
profiles:
active: testing
After merge i want to have:
spring.profiles.active=testing
my.test.value=test
Currently it's failed with error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'my.test.value' in value "${my.test.value}"
Use spring.profiles.include
spring:
profiles:
active: testing
include: default
Or you can create different application-profile.yml (for example application-testing.yml) and specify the profile while starting spring boot application
Command
java -jar name.jar spring.profiles.active=testing // it will pick the properties values from application-testing.yml
In the yml file use include to include any other profiles
application-testing.yml
spring:
profiles:
include: default
#Deadpool
One issue with override
I made application-starter.yaml in starter and include it in sprinng-boot with include.
my:
test:
value: spring--123
spring:
profiles:
active: development
include: starter
I injected with #Value
#Value("${my.test.value}")
private String testval;
and testval=test (from starter) Have i option for make values from application.yaml more primary?
thanks
Related
In our config server we have following application properties files:
helloApp-dev.properties //dev env properties
helloApp-commonConfig.properties //common properties across env
The properties are accessible from URI like:
https://myapp.abc.com/1234/helloApp-dev.properties
https://myapp.abc.com/1234/helloApp-commonConfig.properties
Below is our bootstrap.yml of helloApp application:
---
spring:
application:
name: helloApp
---
spring
profiles: dev
cloud:
config:
label: 1234
name: {spring.application.name}
uri: https://myapp.abc.com/
I am using Spring boot version 2.2.4. The helloApp-dev.properties are loading successfully in application but not commonConfig.
The configuration in commonConfig is not being loaded because you are not indicating to Spring that it should load that profile/config, because you are activating only the dev profile - and the configuration for that profile is the one which is being loaded:
---
spring
profiles: dev
In addition to the good solutions proposed by #akozintsov in his/her answer, if you need to include certain common configuration in different environments or profiles, let's say dev, qa or prod, you can use the spring.profiles.include configuration property and include, as a comma separated list of values, if using properties files, or as a list, if using yaml, the different common profiles and corresponding configurations you need to.
In your example, in helloApp-dev.properties you need to include the following information within the rest of your configuration:
spring.profiles.include=commonConfig
These related SO question and this article could be of help as well.
To load properties file, profiles should match.
You have 2 solutions:
Rename helloApp-commonConfig.properties -> helloApp.properties
Use multiple profiles for your application (dev, commonConfig)
When I was working on a Spring Boot application that used scheduling and acted as a config client, I was unable to pull the crontab expression from the application-cloud.yml file.
This:
#Scheduled(cron = "${cron.expression}")
did not work.
Putting the cron.expression in the jar's internal application.yml was not an option. As a workaround, I changed the source of that property to the appropriate environment variable, CRON_EXPRESSION.
Does anyone know why I had to do that? Why was Spring Scheduled unable to resolve an externalized configuration property?
Edit:
Here is the bootstrap.yml that the project was setup with:
spring:
profiles: cloud
application:
name: application-name
cloud:
config:
uri: https://config-server.com
fail-fast: false
I have a Spring boot application which is managed by Gradle.
What is done so far:
Created application.yaml, application-dev.yaml, and application-qa.yaml files
Mentioned spring: profiles: active: dev in application.yaml (default profile should be dev if none is mentioned)
What need to be done:
Need to build war with profile mentioned or passed while building. Example, for QA build, spring: profiles: active: should have value qa, so that it can pick-up application-qa.yaml properties.
If no profile is passed, then by default dev profile should be selected
What is tried so far:
Added #activeProfiles# in spring: profiles: active: #activeProfiles# in application.yaml and added below snippet in build.gradle:
processResources {
filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
activeProfiles: activeProfiles
]
}
And fired following command to build the war - gradlew clean build -PactiveProfiles=qa, and it just did right job for war file. But now issue with this approach is, how to provide default value for activeProfiles while running the project from IDE (I usually prefer to run main class from IntelliJ)?
I am also not sure if this is the correct approach or is there any other approach to achieve this task. I have already done all these stuff with Maven, and with ease, but I am new to Gradle, and it's project requirement to use Gradle.
One common approach to switch between profiles in different environments is to set up SPRING_PROFILES_ACTIVE environment variable to your desired value and let Spring boot use that. For example, in QA environment you would set the value to qa and Spring will automatically use qa application properties.
Read more : https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles
I was able to set default profile, if no profile is passed while running/building the code, using below configurations in build.gradle file. No extra configuration is needed in any IDE to run the project.
def activeProfiles=project.properties['activeProfiles'] ?: "dev" // set default (dev) profile if no profile is passed as property
processResources {
filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
activeProfiles: activeProfiles
]
}
I have 3 yaml files for configurations as per environments:
application.yaml
application-dev.yaml
application-qa.yaml
application.yaml contains below configuration for spring profile management, and other common configurations:
spring:
profiles:
active: #activeProfiles#
...
application-dev.yaml and application-qa.yaml files contain configurations related to respective environments.
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{}
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.