Member-only story
Spring Boot — Override default properties
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.enabled
it’s value istrue
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
- 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…