Using `@ConfigurationProperties` annotation on `@Bean` Method

前端 未结 4 1768
栀梦
栀梦 2020-12-14 16:38

Could someone give a MWE of how to use the @ConfigurationProperties annotation directly on a @Bean method?

I have seen countless examples o

4条回答
  •  天命终不由人
    2020-12-14 17:02

    24.8.1 Third-party Configuration

    As well as using @ConfigurationProperties to annotate a class, you can also use it on public @Bean methods. Doing so can be particularly useful when you want to bind properties to third-party components that are outside of your control.

    To configure a bean from the Environment properties, add @ConfigurationProperties to its bean registration, as shown in the following example:

    @ConfigurationProperties(prefix = "another")
    @Bean
    public AnotherComponent anotherComponent() {
        ...
    }
    

    Any property defined with the another prefix is mapped onto that AnotherComponent bean in manner similar to the preceding AcmeProperties example.

提交回复
热议问题