Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Reading Configuration Files in Spring Boot

Notes May 13 1

Spring Boot Configuration Files

Spring Boot supports two types of configuration files: core configuration files and custom configuration files. Core configurasion files are typically named application.properties or application.yml and reside in the resources directory. To maintain the integrity of core files while adding custom configurations, developers often create separate custom configuration files. For example, you can create config.properties in the resources/config directory.

Reading Core Configuration Files

Consider the following YAML configuration:

server:
  port: 8081
  servlet:
    context-path: /boot

application:
  title: demo-service

Method 1: Using @Value Annotation

@RestController
public class DemoController {

    @Value("${application.title}")
    private String appTitle;

    @GetMapping("/info")
    public String getInfo() {
        return "Application title: " + appTitle;
    }
}

Method 2: Using Environment Interface

@RestController
public class DemoController {
    
    @Autowired
    private Environment environment;

    @GetMapping("/info")
    public String getInfo() {
        return "Application title: " + environment.getProperty("application.title");
    }
}

Reading Custom Configuration Files

Custom configuration file config.properties content:

build.version=2.0.0
build.artifact=spring-app

Create a configuration class with property mapping:

@Component
@ConfigurationProperties(prefix = "build")
@PropertySource("classpath:config/config.properties")
public class BuildConfig {
    private String version;
    private String artifact;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getArtifact() {
        return artifact;
    }

    public void setArtifact(String artifact) {
        this.artifact = artifact;
    }
}

Using the custom configuration in a controller:

@RestController
public class DemoController {

    @Autowired
    private BuildConfig buildConfig;

    @GetMapping("/build")
    public String getBuildInfo() {
        return "Version: " + buildConfig.getVersion() + 
               ", Artifact: " + buildConfig.getArtifact();
    }
}

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.