Sitemap
Niharika

Niharika

Software Developer - Angular, Spring Boot

Spring Boot Configuration

6 min readJul 15, 2024

--

Let’s explore configuring a spring boot application

Use Cases:

  • Database connections
  • Credentials
  • Feature flags
  • Business logic configuration parameters
  • Scenario testing
  • Spring Boot configuration

Different types of config / Ways to set property values

  • Properties, YAML, JSON

Using Properties files — application.properties

  • syntax : propertyName=value
application.properties

Using YAML files — application.yml

  • Yet Another Markup Language / Ain’t Markup Language
  • .yml or .yaml extn
  • Syntax — propertyName:value (replace = with : and can nest, key:value)
  • nesting structure
  • intend — spaces, no tabs
application.yml

To learn more about key-value pairs, objects, lists, comments,.. of yaml in 15 min , do checkout: https://www.youtube.com/watch?v=1uFVr15xDGg

Ways to get property values

  • @Value annotation
  • @ConfigurationProperties annotation
  • Environment Object (not recommended)

1) Using Internal property sources with Spring Boot

  • @Value — used for injecting values into fields in Spring-managed beans, primarily used for assigning default values to variables and method parameters.
  • Present inside the same jar of the project

Three Value annotation tricks you should know:

  • Specify default value in case of no property value to avoid error
  • Managing Lists
  • Managing Maps(key value pairs)
application.properties
GreetingController.java

Result:

2) Using external property sources with Spring Boot

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
application.properties in target folder
Output

3) Using Command line property source

Press enter or click to view image in full size

4) Set Environment Variables & Read the variable

4.1 Using System Environment Variables

Get Niharika’s stories in your inbox

Join Medium for free to get updates from this writer.

cmd->system environments

Press enter or click to view image in full size
setting system variables
application.properties
GreetingController.java
Output

4.2 Using .env

Create a .env file in resources (is invisible)

.env

Now set value in properties file as,

application.properties

Add spring-dotenv dependency in pom.xml for enabling spring to read properties from .env file

pom.xml

Test the value in controller,

GreetingController.java

Hit the url and verify the value,

output

In order to not track the value changes in .env add .env file in .gitignore

.gitignore

5) ConfigurationProperties

@ConfigurationProperties vs @Value

  • @ConfigurationProperties is used on a POJO bean to map properties to its fields or setters. Then you can use the bean to access the property values in your application logic.
  • @Value is to inject a particular property value by its key into a variable (member field or constructor argument).
  • ConfigurationProperties is useful when you want to pick up group of properties at once and make it available throughout the application. Rather than injecting each value one by one, you can create it as a bean. Annotation to be used is @ConfigurationProperties(“prefix”) and @Configuration To create it as bean. It searches for all the properties starting with the prefix and maps the values with matching member variables. Now Get all the related proprties in one shot with DI @Autowired. Rather than adding properties at multiple places you can make the most used properties with ConfigurationProperties and make it available throughout the application
  • <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
    </dependency>
application.properties
Press enter or click to view image in full size
GreetingController.java
DBSetting.java
Output

refer https://www.baeldung.com/spring-yaml for yaml

Environment Object

  • @Value, @ConfigurationProperties inject value where as Environment Object can looks up at profiles and property values
  • @Autowired private Environment env;
  • Spring provides Environment bean/object which can be autowired
  • Not recommended method as it affects testability. Instead use @Value and ${}

Spring Profiles

  • application-profileName.extn
  • application.properties(default) , appplication-properties.local, application-dev.properties, application-uat.properties, application-prod.properties
  • ex : application-test.yml or application-test.properties creates a profile called test
  • default profile i.e application.yml or application.properties is always active
  • To switch/access the profile, add spring.profiles.active=test in the default profile. Doing this picks application-test.yml file properties
  • Basically both default and test profiles are active. Here test is written on top of default .So property values in test overrides default
  • So common properties can be put in default property files and env specific properties in env specific profiles
Press enter or click to view image in full size
Press enter or click to view image in full size
  • you can override the active profile present in default profile in respective jar through command line argument
Press enter or click to view image in full size

Selecting Beans by profile

Press enter or click to view image in full size

Bean selection based on @Profile annotation, else without @Profile , @Profile(”default” ) is annotated by default which means all beans are selected

application.properties vs application.yml

Press enter or click to view image in full size

https://www.javaguides.net/p/properties-to-yaml-converter-online.html

source code for reference: https://github.com/D-Palamala-Sai-Niharika/configuring-springboot-application/

Sources referred: Java Brains

Niharika
Niharika

Written by Niharika

Software Developer - Angular, Spring Boot