Spring profile properties inheritance with single properties file - spring-boot

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.

Related

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

Default value for profiles in Spring Boot

I have multiple profiles and there are lots of attributes(which will grow), I don't want to set each attribute every time in a different profile.
I have an application.yml file like this:
freemarker:
template-loader-path: classpath:/templates
datasource:
username: postgres
password: mypass
driver-class-name: org.postgresql.Driver
jpa:
show-sql: true
properties:
hibernate:
jdbc:
lob:
non_contextual_creation: true
dialect: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: create
security:
secret: "jwt_secret_key_it_is_a_random_key_229"
loginTokenExpiration: 86400
confirmUserTokenExpiration: 86400
devTokenExpiration: 157680000
tokenPrefix: "Bearer"
headerString: "Authorization"
signUpUrl: "/token/login"
mysite:
apiTosUrl: "https://example.com/api-tos"
fromEmail: "alert#example.com"
firstFreeCredits: 10
junction:
port: 9080
hasBasicAuth: false
---
spring:
profiles:
active: dev
---
spring:
profiles: stage
jpa:
show-sql: true
hibernate:
ddl-auto: update
chargeBee:
site: "example.chargebee.com"
apiKey: "mykey"
---
spring:
profiles: prod
datasource:
url: jdbc:postgresql://localhost/myproddb
jpa:
show-sql: false
hibernate:
ddl-auto: update
chargeBee:
site: "example.chargebee.com"
apiKey: "myapikey"
Most of the settings are common between profiles, and which are not common, I have re-defined it in the corresponding section of the profile.
I am assuming that the first section in this YAML document fills the properties with default values and the corresponding sections overwrite it.
Is this approach correct? If not then how to have an inheritance of properties values such that I define the common value once and for the rest of the profiles, I only need to define the different values?
you can also achieve profile using create the different profile-specific yml file.
you can also be defined by using the following naming convention:
application-{profile}.yml
Create application.yml file. This file loaded first. Load all attribute of application.yml file.
Check any active profile found(yes stage) then load after successfully load default profile then load application-stage.yml file and override existing attribute and add the new attribute.
spring:
profiles:
active: stage
datasource:
url: jdbc:postgresql://localhost:5432/springbootdb
create application-stage.yml file same location of application.yml.
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
chargeBee:
site: "example.chargebee.com"
apiKey: "mykey"
create application-prod.yml file same location of application.yml.
spring:
datasource:
url: jdbc:postgresql://localhost/myproddb
jpa:
show-sql: false
hibernate:
ddl-auto: update
chargeBee:
site: "example.chargebee.com"
apiKey: "myapikey"
Yes that correct approach to define multiple profiles
You can specify multiple profile-specific YAML documents in a single
file by using a spring.profiles key to indicate when the document
applies, as shown in the following example:
See official document 24.6.3 Multi-profile YAML Documents
In the below example, if the development profile is active, the server.address property is 127.0.0.1. Similarly, if the production profile is active, the server.address property is 192.168.1.120. If the development and production profiles are not enabled, then the value for the property is 192.168.1.100.
server:
address: 192.168.1.100
---
spring:
profiles: development
server:
address: 127.0.0.1
---
spring:
profiles: production
server:
address: 192.168.1.120

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?

spring boot yaml profile names resolution

I have multimodule maven project in which I have one common part which I want to share between applications.
In common module I'm defining the configuration for liquibase and db connection. For this I have 2 profiles common-prod and common-dev located in application.yaml.
spring:
profiles: common-prod
# POSTGRE
datasource:
url: ${DATABASE_URL:jdbc:postgresql://localhost:5432/dbname}
driver-class-name: org.postgresql.Driver
username: pguser
password: pguser
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: none
properties:
hibernate.default_schema: public
#LIQUIBASE
liquibase:
enabled: true
change-log: /db/changelog/db-changelog-master.xml
drop-first: false
---
spring:
profiles: common-dev
# POSTGRE
datasource:
url: ${DATABASE_URL:jdbc:postgresql://localhost:5432/dbname-dev}
username: pguserdev
password: pguserdev
#LIQUIBASE
liquibase:
enabled: true
drop-first: true
now in web application I want to define some additional properties. But when I include them in application.yaml (in web module) I think it overrides the application.yaml from common module.
So I need to
rename the application.yaml from common to something like application-common.yaml
rename the application.yaml in application to something like application-web.yaml
if I rename the common module's yaml file am I able to reference the profiles from within this file? eg something like -Dspring.profiles.active=common-common-prod or I need to separate this profiles to separate yaml files?
If I rename web modules config file to application-web.yaml I must allways include the web profile. I can live with that, but it would be simpler to include the profiles in application.yaml and don't care about command line arguments.

Resources