Procedure to make the value of the property file visible in Spring Boot

Introduction

This page is mainly for your own memory.

procedure

If you write it very roughly, it will be in the following order.

    1. Write the value you want to set in the property file
  1. Create a settings read class
    1. Use with Autowired

Property file (application.yml)

For example, if you have the following application.yml

ymlSetting:
  stringKey: "Some kind of string"
  mapA:
    key1: "value1"
    key2: "value2"

Class to read property file

Create a class to read and store values from application.yml. Set the application.yml key to the prefix of the "@ConfigurationProperties" annotation. Then, you can define that this class will read this definition. (In the example, the Setting class defines that the setting value under ymlSetting is read.)

Each defined item is stored as a member variable. The non-nested item (stringKey) is of type String Items with child elements (mapA) can be declared as Maps.

When referencing the value in the property file, the value will be referenced using the Getter of the declared member variable.

Settings.java

package jp.co.sample;
 
import java.util.Map;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "ymlSetting")
public class Settings {
    private String stringKey;
    private Map<String, String> mapA;
     
    public void setStringKey(String stringKey){
        this.stringKey = stringKey;
    }
     
    public String getStringKey(){
        return stringKey;
    }
     
    public void setMapA(Map<String, String> mapA){
        this.mapA = mapA;
    }
     
    public Map<String, String> getMapA(){
        return mapA;
    }
}

Usage example

For example, when the property value is output as standard, it looks like this.

SpringBootConfigApplication.java

package jp.co.sample;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SpringBootConfigApplication implements CommandLineRunner{
    @Autowired
    private Setting setting; //Property file reading class
     
  public static void main(String[] args) {
    SpringApplication.run(SpringBootConfigApplication.class, args);
  }
   
  @Override
    public void run(String... args) {
        System.out.println("string = " + Settings.getStringKey());
        System.out.println("key1 = " + Settings.getMapA().get("key1"));
        System.out.println("key2 = " + Settings.getMapA().get("key2"));
    }
}

Recommended Posts

Procedure to make the value of the property file visible in Spring Boot
How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
How to set environment variables in the properties file of Spring boot application
Extract SQL to property file with jdbcTemplate of spring boot
Sample code to assign a value in a property file to a field of the expected type
The story of raising Spring Boot 1.5 series to 2.1 series
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
How to get the setting value (property value) from the database in Spring Framework
[Spring Boot] List of validation rules that can be used in the property file for error messages
Specify the encoding of static resources in Spring Boot
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
How to use CommandLineRunner in Spring Batch of Spring Boot
The story of raising Spring Boot from 1.5 series to 2.1 series part2
About the function of Spring Boot due to different versions
Put the file in the properties of string in spring xml configuration
How to specify an array in the return value / argument of the method in the CORBA IDL file
I want to control the maximum file size in file upload for each URL in Spring Boot
Branch processing by the return value of RestTemplate and the status code of ResponseEntity in Spring Boot
It doesn't respond to the description in .js of the packs file
Get a proxy instance of the component itself in Spring Boot
Fix the file name of war to the one set in Maven
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
I want to control the default error message of Spring Boot
Form that receives the value of the repeating item in Spring MVC
I want to change the value of Attribute in Selenium of Ruby
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
I tried to make the "Select File" button of the sample application created in the Rails tutorial cool
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
Static file access priority in Spring boot
Local file download memorandum in Spring Boot
How to split Spring Boot message file
I tried to make a parent class of a value object in Ruby
[Spring Boot] I investigated how to implement post-processing of the received request.
The story of forgetting to close a file in Java and failing
Sample program that returns the hash value of a file in Java
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
How to change the value of a variable at a breakpoint in intelliJ
I tried to make full use of the CPU core in Ruby
Android development, how to check null in the value of JSON object
Make a margin to the left of the TextField
How to make Spring Boot Docker Image smaller
How to check the latest version of io.spring.platform to describe in pom.xml of Spring (STS)
Procedure to change lower_case_table_names = 1 in MySQL 8.0 of CentOS 8.3
Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf
Let's check the feel of Spring Boot + Swagger 2.0
Sign in to a Spring Boot web application on the Microsoft ID platform
How to add a classpath in Spring Boot
Get the path defined in Controller class of Spring boot as a list
Make the JSON of the snake case correspond to the field of the camel case in Java (JVM)
How to make a unique combination of data in the rails intermediate table
The attached file name was garbled in the Spring Boot email, so take measures
View the Gradle task in the Spring Boot project
I want to get the value in Ruby
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
I want to read the property file with a file name other than application.yml or application- [profile name] .yml with Spring Boot.
Install multiple submit buttons in Rails View to get the value of the pressed button
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA