Multi-document file in Spring Boot 3

453 Views Asked by At

I wanted to create a multi-document application.properties in Spring Boot REST API. I wanted to create two profiles (prod and dev) with spring.data.rest.limit-param-name property with value 5 for prod and 100 for dev. Actually, IntelliJ informes me about duplicate property key, even though I try to follow this guidance:

Spring Boot docs

My code looks like that:

spring.profiles.active=prod,dev
spring.data.rest.limit-param-name=5
#---
spring.config.activate.on-profile=dev
spring.data.rest.limit-param-name=100
2

There are 2 best solutions below

0
Pu6 On BEST ANSWER

Answer to my own question:

It appeared that I did not provided accurate lines, so the file did not want to run. Here is the answer:

spring.data.rest.limit-param-name=5
spring.profiles.active=dev
#---
spring.config.activate.on-profile=dev
spring.data.rest.limit-param-name=100
1
Fergus On

This is due to Spring Boot's multi-profile configuration. If you want to use profile-specific properties in a single application.properties or application.yml file, you have to use the spring.profiles property in what i think you are probably using which is spring.config.activate.on-profile.

application.properties:

  1. Using separate application-{profile}.properties files: This is the common way to manage profile-based configurations.

application.properties

spring.profiles.active=prod

application-dev.properties

spring.data.rest.limit-param-name=100

application-prod.properties

spring.data.rest.limit-param-name=5
  1. Using only one application.properties file: If you still want to have everything in one file, you should switch to the application.yml format, which supports multi-document YAML files.

application.yml

spring:
  profiles:
    active: prod,dev

---

spring:
  profiles: dev
  data:
    rest:
      limit-param-name: 100

---

spring:
  profiles: prod
  data:
    rest:
      limit-param-name: 5

If you choose the first method, depending on the active profile, Spring Boot will load the main application.properties followed by application-{profile}.properties.

If you choose the second method, the application.yml will activate properties based on the profile due to the multi-document YAML structure.