How to customize configuration properties in Spring Boot

In Spring Boot, you can customize configuration properties by creating a class annotated with @ConfigurationProperties. Here is an example:

  1. Properties configuration
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "custom")
public class CustomProperties {
    private String property1;
    private int property2;

    // 省略getter和setter方法
}
  1. config file for the application
  2. settings file for an application
custom.property1=value1
custom.property2=123
  1. Unique characteristics or attributes
  2. properties file used for configuring an application
  3. Properties that are customized or tailored to specific needs
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomController {

    @Autowired
    private CustomProperties customProperties;

    @GetMapping("/properties")
    public String getProperties() {
        return "Property1: " + customProperties.getProperty1() + ", Property2: " + customProperties.getProperty2();
    }
}

This way, you can customize configuration properties and use them in SpringBoot.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds