24.7.1. 第三方配置

@ConfigurationProperties不仅可以注解在类上,也可以注解在public @Bean方法上,当你需要为不受控的第三方组件绑定属性时,该方法将非常有用。

为了从Environment属性中配置一个bean,你需要使用@ConfigurationProperties注解该bean:

@ConfigurationProperties(prefix = "foo")
@Bean
public FooComponent fooComponent() {
    ...
}

和上面ConnectionSettings的示例方式相同,所有以foo为前缀的属性定义都会被映射到FooComponent上。

Last updated