Member-only story

Spring Boot — Override default properties

Karanbir Singh
2 min readMay 25, 2024

Spring boot is a very opinionated framework & there are too many libraries working on the similar model which is based on opinions.

The library - spring doc for example is a very famous library responsible to generate and expose the api specifications and swagger ui.

Example Scenario - conditionally disable the exposure of the api specification & swagger UI.

The details of the scenario are:-

  • in the production and pre prod environments the spring doc functionality should not expose the swagger specs & UI.
  • it relies on a property which is - springdoc.api-docs.enabledit’s value is true by default.
  • for environment profiles other than local, dev, uat, etc. the property should be set to false even if developer(s) set it true accidently.

Solution

For conditionally overriding a property we can use the Application listener and configure that in the spring.factories file

  1. Create a file as below:-
package in.neuw.artifact.listeners;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import…

--

--

Karanbir Singh
Karanbir Singh

Written by Karanbir Singh

API developer + Web Application developer + Devops Engineer = Full Stack Developer

No responses yet