How can use ConfigDataEnvironmentPostProcessor and spring.profiles.group? - spring

I'm using Spring Boot 2.4.2 and tried to use spring.profiles.group in my property files like:
application.yaml
spring:
profiles:
group:
local: core
application-core.yaml
---
spring:
config:
activate:
on-profile: local
...
but the "group" property is not found.
As I checked, it is using deprecated ConfigFileApplicationListener instead of ConfigDataEnvironmentPostProcessor.
Why it uses legacy spring profile processing even though version of spring is upper 2.4.0?

application-core.yaml doesn't require the following:
---
spring:
config:
activate:
on-profile: local
This is what is needed:
application.yaml:
spring:
profiles:
group:
local: core
application-core.yaml
...

Related

spring boot 3 profile property values getting overridden in boostrap.yaml

Recently trying to upgrade to Spring Boot 3, but facing property issue.
My bootstrap yml looks like below,
When I run with profile dev, the configserver url is always getting connected to qa, basically whichever is declared last.
This is working fine in 2.7.X version of spring, but not in spring boot 3
spring:
application:
name: appname
profiles:
active: ${spring.profiles.active}
include:
- global-defaults
group:
dev:
- config-server-dev
- cloud-config-dev
qa:
- config-server-qa
- cloud-config-qa
---
spring:
config:
activate:
on-profile: config-server-dev
cloud:
config:
enabled: true
uri: http://urltoconfigserverdev
label: master
---
spring:
config:
activate:
on-profile: config-server-qa
cloud:
config:
enabled: true
uri: http://urltoconfigserverqa
label: master
---
But the same is working fine if add the property migration pom dependency. which is not suggested for higher environments.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>
If adding the properties migrator fixes the problem, it's likely that your configuration file is using a configuration property name that's been removed after deprecation. I'm not seeing anything specific to Spring Boot in the changelog, maybe this is due to a change in spring cloud config? The properties migrator should print WARN logs during startup, looking at those should tell you what needs fixing.

Spring cloud config not apply global configuration

I'm having a problem on spring cloud configuration 2020.0.3, spring boot 2.4.5 for details:
Yaml configuration file is following Multi-profile YAML documents
I have a configuration yaml file on the config server.
my_cofig.yaml
spring:
datasource:
url: "jdbc:mariadb://localhost:3306/default_db"
driver-class-name: org.mariadb.jdbc.Driver
username: my_db
password: 12345
---
spring:
profiles: dev
datasource:
url: "jdbc:mariadb://localhost:3306/dev_db"
I have loaded the config from the config server by browser, it's correct.
But:
When I run Spring application with specific configuration (e.g. dev), Spring application must not apply global configuration variables defined on configuration file from configuration server. It only loads dev's configuration variables.
bootstrap.yaml
server:
port: 8081
spring:
application:
name: auth-service
cloud:
config:
enabled: true
fail-fast: true
allow-override: true
profile: dev
uri: http://localhost:5000
Error detail:
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Please help me, many thanks!
spring:
profiles:
This does not exist (it was deprecated if I am not mistaken). Please check the reference documentation.
I believe that what you are looking for is the following (reference documentation):
spring:
datasource:
url: "jdbc:mariadb://localhost:3306/default_db"
driver-class-name: org.mariadb.jdbc.Driver
username: my_db
password: 12345
---
spring:
config:
activate:
on-profile: dev
datasource:
url: "jdbc:mariadb://localhost:3306/dev_db"
Finaly, I solved the problem by upgrading spring boot version to 2.5.1
Thanks all

How can I activate Profile in Spring Boot YAML?

I have below application YAML of my Spring Boot (2.4.5) application with two profiles.
spring:
profiles: dev
spring:
profiles: prod
Now when I pass the arguments -Dspring.profiles.active=dev, it actually doesn't pickup the profile. Is there any changes in profile has been made recently.
When I activate the profile, like below it works
spring:
profiles:
active: dev
But as per the env profile can be different. I don't want to hard code the profile
You should use the following instead in your application.yml:
spring
config:
activate:
on-profile: dev
---
spring
config:
activate:
on-profile: prod

Spring profile properties inheritance with single properties file

I have one application.yml file containing multiple spring profiles.
I want to inherit properties from one profile to another.
In this example I want to inherit properties of prod profile into prod1 without writing common properties again in prod1 profile.
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
application:
name: TestApp
URL: "https://localhost:8181/Services/IDEA-Client-Partners"
---
spring:
profiles: dev
---
spring:
profiles: prod
URL: https://www.ideaedu.org/Services/IDEA-Client-Partners
---
spring:
profiles: prod1
properties do already inherit if multiple profiles are activate. E.g. if you activate prod, and prod1 all default < prod < prod1 properties will activate with default, getting overwritten by anything in prod, and prod getting overwritten by anything in prod1.
Given your example,
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
application:
name: TestApp
URL: "https://localhost:8181/Services/IDEA-Client-Partners"
---
spring:
profiles: dev
---
spring:
profiles: prod
URL: https://www.ideaedu.org/Services/IDEA-Client-Partners
prodProperty: test
---
spring:
profiles: prod1
URL: https://localhost/
And activating all profiles, -Dspring.profiles.active=prod,prod1
the following properties will be set,
port = 8080
diver-class-name= com.mysql.cj.jdbc.Driver
name = TestApp
prodProperty = test
URL = https://localhost/
In case of conflicting properties e.g. URL in this example, the last property read wins i.e. when prod and prod1 are active the last property read will in, prod1's definition in this case.

Spring cloud composite bootstrap.yml

Is there any way how to "build" bootstrap.yml from multiple yaml files?
Here is my scenario, I have a bunch of micro-services and every service had the same bootstrap.yml properties expect spring.application.name.
I would like to split my bootstrap.yml into two files, base-bootstrap.yml which contains config service configuration like URI, password ... etc and then bootstrap.yml which could contain spring.application.name or whatever else.
base-bootstrap.yml file could be externalized in some dependency jar and should be used like default, bootstrap.yml should override any value. Here is some sample:
base-boostrap.yml
spring:
cloud:
config:
label: develop
uri: http://config
password: ${CONFIG_SERVICE_PASSWORD:password}
fail-fast: true
---
spring:
profiles: production
cloud:
config:
label: master
password: ${CONFIG_SERVICE_PASSWORD}
---
spring:
profiles: development
cloud:
config:
uri: http://localhost:8888
bootstrap.yml
spring:
application.name: sample-service
cloud:
config:
label: release/1.0.1
---
spring:
profiles: production
cloud:
config:
label: 1.1.0
Could anyone please guide me how to do it?

Resources