Skip to main content

Command Palette

Search for a command to run...

Setting Up Complex Configurations for Spring Boot applications

Published
2 min read
Setting Up Complex Configurations for Spring Boot applications
J

I've 3.5+ years of experience in software development and willing to share my knowledge while working on real-time IT solutions.

Introduction

In Spring Boot, the service configurations defined in application.properties file are automatically bind to Java objects using "@ConfigurationProperties" annotation. It helps you centralize the configurations and easily inject properties into Spring Components. It provides a clean and maintainable way to manage configuration properties.

Steps to use Service Configurations

  1. Define the properties in application.properties file
app-service.url=http://default.com/service
app-service.timeout=7000
app-service.retryAttempts=4
  1. Create a ServiceConfig.java class and annotate it with "@ConfigurationProperties". The prefix used for properties can also be specified as below.
package com.example.webstarter;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix="app-service")
@Component
public class ServiceConfig {

    private String url;
    private String timeout;
    private String retryAttempts;

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getTimeout() {
        return timeout;
    }
    public void setTimeout(String timeout) {
        this.timeout = timeout;
    }
    public String getRetryAttempts() {
        return retryAttempts;
    }
    public void setRetryAttempts(String retryAttempts) {
        this.retryAttempts = retryAttempts;
    }
}
  1. Create a Controller to retrieve service configuration properties mapped to endpoint http://localhost:8080/getServiceConfigurations
package com.example.webstarter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceConfigController {

    @Autowired
    private ServiceConfig serviceConfig;

    @RequestMapping("/getServiceConfigurations")
    public ServiceConfig getServiceConfigurations() {
        return serviceConfig;    
    }
}
  1. Run the main class annotated with "@SpringBootApplication" in your project.
package com.example.webstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebstarterApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebstarterApplication.class, args);
    }

}

Output

{
  "url": "http://default.com/service",
  "timeout": "7000",
  "retryAttempts": "4"
}

Summary

This article describes how to centralize and manage service configurations in Spring Boot using the `@ConfigurationProperties` annotation to automatically bind properties defined in `application.properties` to Java objects. This tutorial guides you through defining properties, creating a configuration class, setting up a controller to expose the configurations via a REST endpoint, and running a Spring Boot application to retrieve and display the configured properties.

More from this blog

Untitled Publication

21 posts