SoFunction
Updated on 2025-03-02

Steps to define and read custom configurations in SpringBoot

Preface

Defining and reading custom configurations in Spring Boot is a common requirement in daily development, allowing us to manage the configuration information of our application in a flexible way, whether through external configuration files (such as or) or through environment variables.

As senior programmers, we need to master this skill to ensure the configurability and maintainability of our applications. Here is a detailed step description, including sample code, showing how to define and read custom configurations in Spring Boot.

In Spring Boot, you can define and read custom configurations through the following steps:

  • existorCustom configuration items are defined in .

For example,Added in:

app:
  custom:
    my-property: value
  • Create a configuration class to bind these properties.

import ;
import ;
 
@Configuration
@ConfigurationProperties(prefix = "")
public class CustomProperties {
 
    private String myProperty;
 
    public String getMyProperty() {
        return myProperty;
    }
 
    public void setMyProperty(String myProperty) {
         = myProperty;
    }
}
  • Injection in Spring BeanCustomPropertiesclass to use configuration values.

import ;
import ;
 
@Component
public class MyComponent {
 
    private final CustomProperties customProperties;
 
    @Autowired
    public MyComponent(CustomProperties customProperties) {
         = customProperties;
    }
 
    public void printCustomProperty() {
        (());
    }
}

make sure@ConfigurationPropertiesAnnotatedprefixThe attribute matches the prefix you defined in the configuration file. Spring Boot will automatically bind configuration properties toCustomPropertieson the field of the class. You can then use these configuration values ​​in any part of the application through automatic assembly.

or:

Define custom configuration properties

# 
=Spring Boot Application
=This is a demo application for custom configuration
=8081

Create a configuration class

Next, we need to create a configuration class to bind these custom properties. In Spring Boot, this usually passes@ConfigurationPropertiesAnnotation implementation, which allows us to bind properties in configuration files to JavaBeans.

import ;
import ;
 
@Configuration
@ConfigurationProperties(prefix = "myapp")
public class MyAppConfig {
 
    private String name;
    private String description;
    private int serverPort;
 
    // Standard getter and setter methods    public String getName() {
        return name;
    }
 
    public void setName(String name) {
         = name;
    }
 
    public String getDescription() {
        return description;
    }
 
    public void setDescription(String description) {
         = description;
    }
 
    public int getServerPort() {
        return serverPort;
    }
 
    public void setServerPort(int serverPort) {
         = serverPort;
    }
}

Note that the @ConfigurationProperties(prefix = "myapp") annotation specifies that the configuration property prefix is ​​myapp, so that Spring Boot can automatically bind all properties starting with myapp to the corresponding fields of the MyAppConfig class.

Read configuration

We can read these configurations through dependency injection anywhere in the application. For example, in a Controller

import ;
import ;
import ;
 
@RestController
public class MyAppConfigController {
 
    private final MyAppConfig myAppConfig;
 
    @Autowired
    public MyAppConfigController(MyAppConfig myAppConfig) {
         = myAppConfig;
    }
 
    @GetMapping("/config")
    public String getConfig() {
        return "Name: " + () + ", Description: " + () + ", Server Port: " + ();
    }
}

Summarize

Through the above steps, we successfully defined custom configuration properties in Spring Boot and bound them to JavaBean via the @ConfigurationProperties annotation. Finally, we read these configurations in the application through dependency injection. This method not only improves the readability and maintainability of the code, but also makes configuration management more flexible and convenient. In actual development, rationally utilizing Spring Boot's configuration management function can greatly improve the flexibility and scalability of the application.

The above is the detailed content of the methods and steps for defining and reading custom configurations in SpringBoot. For more information about SpringBoot definition and reading configurations, please pay attention to my other related articles!