要自定義Spring的@ConfigurationProperties,可以按照以下步驟進行:
@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
private String property1;
private int property2;
// 省略getter和setter方法
}
myapp.property1=value1
myapp.property2=123
@Configuration
@EnableConfigurationProperties(MyAppProperties.class)
public class AppConfig {
@Bean
public MyBean myBean(MyAppProperties myAppProperties) {
return new MyBean(myAppProperties.getProperty1(), myAppProperties.getProperty2());
}
}
通過以上步驟,就可以自定義Spring的@ConfigurationProperties,并在程序中使用這些自定義的配置值了。