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:
- 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方法
}
- config file for the application
- settings file for an application
custom.property1=value1
custom.property2=123
- Unique characteristics or attributes
- properties file used for configuring an application
- 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.