要自定义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,并在程序中使用这些自定义的配置值了。