application-profile.yml does not pickup properties from main application.yml - spring-boot

I have a Spring boot application with different environments. I have a main application.yml with a set of properties and an application-test.yml for my test environment.
Inside my application.yml, I have the following config
spring:
liquibase:
enabled: false
user: ${SPRING_DATASOURCE_USERNAME}
password: ${SPRING_DATASOURCE_PASSWORD}
change-log: classpath:db/changelog-master.yaml
And inside my application-test.yml, I have the following
spring
liquibase:
user: sa
password: password
I was expecting my test environment to use liquibase.user and liquibase.password from the application-test.yml and pick the two other sub-properties (liquibase.enabled and liquibase.change-log) from application.yml. My reasoning was that in my test environment, the Application context would pick up all the properties in application.yml and then overwrite only the subkeys defined in application-test.yml, while keeping the original values of application.yml if not explicitly overwritten.
But that does not seem to be the case; it looks as if the key liquibase in my application-test.yml overwrites all the key and subkeys of the liquibase defined in application.yml.
Therefore since in my application-test.yml, liquibase.enabled and liquibase.change-log are not defined; the test environment does not know about these values. I was expecting them to be picked up from the main application.yml instead. I tried to define them manually in my application-test.yml and my tests work fine. If I remove them, my tests fail because they use the default properties for liquibase instead of liquibase.enabled: false and liquibase.change-log: classpath:db/changelog-master.yaml
What I would like to do is the following:
In my main application.yml, have
main_key:
sub_key_1: value_1
sub_key_2: value_2
sub_key_3: value_3
And in my application-test.yml, have only
main_key:
sub_key_1: test_value_1
have my test environment pickup sub_key_2: value_2 and sub_key_3: value_3 directly from application.yml, without them being overwritten by nothing in my test environment (since they are not defined in my application-test.yml)
Is it possible to have think kind of logic : if main_key.sub_key_2 is defined in application-test.yml is defined, then use it, otherwise use the main_key.sub_key_2 defined in application.yml ?
I feel that if I don't define all the sub_keys of my main_key in my application-test.yml, it is not possible to do it.
Many thanks for your help

Actually, I think I know why it does not work.
I had an empty application.yml inside my test/resources/application.yml. So I believe spring boot picks up the test/resources/application.yml instead of the src/main/resources/application.yml when test/resources/application.yml exists.
Which explains why the properties in my src/main/resources/application.yml were not picked up at all.
The solution was simply to delete the test/resources/application.yml

Related

Understanding the spring profile

I have basic idea how spring profile works. But here in this file this - i am not able to get it. And current Application.yml file mentioning the three profile which one will get active and when that i need to know as well. Below is the Application.yml file content.
spring:
application:
name:
profiles:
active:
-default
-local
-swaggerinfo
Note: i have three config files present in my resources. Also if i want to look another config file
then spring use the naming convention like Application-<Name>.extension . so - is already get added for the new config file then why we explicitly need
to put another one in our application.yml file under spring.profile.active.
Below are the names of the three config files present under the resources folder.
application.yml
application-local.yml
bootstrap-default.yml
But here in this file this - i am not able to get it. spring use the
naming convention like Application-.extension . so - is already
get added for the new config file then why we explicitly need to put
another one in our application.yml file under spring.profile.active
spring:
application:
name:
profiles:
active:
-default
-local
-swaggerinfo
The declaration of profiles are incorrect. You must either put space or should not use (-) at all.
spring:
profiles:
active:
- default
- local
- swaggerinfo
The Spring also supports the following way of declarations.
spring:
profiles:
active: default,local,swaggerinfo
or
spring:
profiles:
active:
default
local
swaggerinfo
Here default refers to application.properties file not bootstrap-default.properties. Also, You don't need to specify the default profile. Spring automatically use application.properties as default one. So, in your case it's appropriate to go with local and swaggerinfo.
current Aplication.yml file mentioning the three profile which one
will get active and when that i need to know as well.
Let's talk about the following declaration.
spring:
profiles:
active:
- local
- swaggerinfo
Both local and swaggerinfo profiles will be active for props loading. So,which means that all the three files (application.yml by default) will be consumed by spring.
Let's talk about the order.
The order in the above case would be
application -> application-local -> application-swaggerinfo
Note:
Assume that you've mentioned the same prop in all the three files then in that case the precedence will be given as per the order highlighted above i.e prop mentioned in the application-swaggerinfo will override the ones available in the other twos.

Spring Application.Properties and Application-dev.properties files parameter conflict

profile logic in my application.properties file.My problem is when I use -dev ,it reads value from application-dev.properties.But I have same key which does not exist in my application-dev.properties but exist on application.properties,spring continue to read values from application.properties
application.properties
myfirstkey=x
mysecondkey=x
application-dev.properties
mysecondkey=dev
-Dspring.profiles.active=dev (// I pass profile value and see result by the way)
Output mysecondkey dev ,it is okay no problem
Output myfirstkey is x (my expectation is empty),but it doesnt exist in application-dev.properties?Is there anyway to prevent application.properties read
Well, application.properties are basically your 'default' .properties and you can't just (on a case-per-case basis) disable them, so the fix would be to do this in your -dev.properties.
myfirstkey=

"java.lang.IllegalStateException: Cannot load driver class" in Spring Boot application

I am using to configure spring boot with an external YAML configuration and CMD.
-> application.yml file
spring:
profiles: integration-test
datasource:
driverClassName: ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}
url: ${SPRING_DATASOURCE_URL}
username: ${SPRING_DATASOURCE_USERNAME}
password: ${SPRING_DATASOURCE_PASSWORD}
-> cmd
mvn clean install
-> Result
Caused by: java.lang.IllegalStateException: Cannot load driver class: ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}
Can anyone explain this to me?
When you use the syntax ${}, you are actually telling Spring Boot to use the value of the property whose name is between brackets. In your case, Spring Boot tries to resolve the property SPRING_DATASOURCE_DRIVER_CLASS_NAME. When it fails, it uses the string as is, which leads to the error you mentioned, since no driver exists under the name ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}.
To solve the issue, you can either :
replace the ${} by the real values, e.g. driverClassName: org.postgresql.Driver and do the same for the other properties (url, username and password)
provide properties SPRING_DATASOURCE_DRIVER_CLASS_NAME,SPRING_DATASOURCE_URL and the two others. These can passed in the command line with -D options (e.g. -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver) or through environment variables. You can look at spring Boot documentation for more details.
Pass those variables in your launch configuration of your program or at commandline when you run your app with java YourMainClass, e.g.
java -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=<full_qualified_name_of_your_jdbc_driver_class> -DSPRING_DATASOURCE_URL=<jdbc_url> YourMainClass
also pass the other two variables the same way, username & password!
your can even set those enviroment variables on OS level, so you don't have to set them each time you start your application...
if your using Spring Boot also have a look at this one: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

throw exceptions for unknown Spring properties

I made a very silly mistake in my Spring Boot app's YAML config:
---
spring:
profiles: local
...
___
spring:
profiles: foo
...
---
spring:
profiles:
active: bar
include: foo
...
I accidentally tried to use "spring.profiles.active" to set the Spring profiles in the last section. Since this does not mean anything to Spring, the last section was always applied and the foo profile was always turned on.
spring.profiles.active is essentially nonsense, yet the app ran without complaint (until the problematic configuration caused other problems).
I would like to configure Spring somehow to immediately throw an exception when it encounters a config parameter that appears internal (spring...) but is unknown to it. How do I do this?

Set logfile location based on OS in spring boot application.properties/.yml

I'm wondering if there's a nice clean way to set logging location based on OS just using the application.properties file in Spring Boot?
For instance is it possible to use a regex matcher on ${os.name} or would I just need to go ahead and create a groovy script or something?
My ideal solution is something like
logging:
file: ${os.name}.test(/*window*/gi) ? C:/ProgramData/Logs/ : /var/log/
You can take advantage of spring profiles and pick configurations according to the -Dspring.profile.active=some_profile system property or SPRING_PROFILES_ACTIVE=some_profile env variable.
Yaml file could be
# a safe default relative to app root
logging:
file: logs
----
spring:
profiles: nix
logging:
file: /var/log/myapp
----
spring:
profiles: win
logging:
file: C:/ProgramData/Logs/
App is executed as
java -Dspring.profile.active=nix <more opts> MyAppMain
or also:
SPRING_PROFILES_ACTIVE=nix java <more opts> MyAppMAin

Resources