要自定义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,并在程序中使用这些自定义的配置值了。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: spring中security怎么获取当前用户