How to autowire @ConfigurationProperties into @Configuration?

前端 未结 1 488
悲&欢浪女
悲&欢浪女 2020-12-19 07:02

I have a properties class defined like this:

@Validated
@ConfigurationProperties(prefix = \"plugin.httpclient\")
public class HttpClientProperties {
   ...
}         


        
相关标签:
1条回答
  • 2020-12-19 07:33

    This is a valid use case, however, your HttpClientProperties are not picked up because they're not scanned by the component scanner. You could annotate your HttpClientProperties with @Component:

    @Validated
    @Component
    @ConfigurationProperties(prefix = "plugin.httpclient")
    public class HttpClientProperties {
        // ...
    }
    

    Another way of doing so (as mentioned by Stephane Nicoll) is by using the @EnableConfigurationProperties() annotation on a Spring configuration class, for example:

    @EnableConfigurationProperties(HttpClientProperties.class) // This is the recommended way
    @EnableScheduling
    public class HttpClientConfiguration {
        // ...
    }
    

    This is also described in the Spring boot docs.

    0 讨论(0)
提交回复
热议问题