Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autowire @ConfigurationProperties into @Configuration?

I have a properties class defined like this:

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

And a configuration class like this:

@Configuration
@EnableScheduling
public class HttpClientConfiguration {

    private final HttpClientProperties httpClientProperties;

    @Autowired
    public HttpClientConfiguration(HttpClientProperties httpClientProperties) {
        this.httpClientProperties = httpClientProperties;
    }

    ...
}

When starting my spring boot application, I'm getting

Parameter 0 of constructor in x.y.z.config.HttpClientConfiguration required a bean of type 'x.y.z.config.HttpClientProperties' that could not be found.

Is this not a valid use case, or do I have to declare the dependencies some how?

like image 765
Daniel Avatar asked May 05 '17 06:05

Daniel


People also ask

Can you Autowire in a configuration class?

In addition to being able to reference any particular bean definition as seen above, one @Configuration class may reference the instance of any other @Configuration class using @Autowired . This works because the @Configuration classes themselves are instantiated and managed as individual Spring beans.

How do you Autowire beans from configuration?

In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file. We have enabled annotation injection.

Where do I put EnableConfigurationProperties?

You can use the @EnableConfigurationProperties to link it to the @ConfigurationProperties class and init your data sources accordingly. @Configuration @EnableConfigurationProperties(DataSourcesConfiguration.

How do you use ConfigurationProperties in a test?

Basically, you: use a specific configuration to @EnableConfigurationProperties and @EnableAutoConfiguration , listing all the @ConfigurationProperties files you want to load. in the test class, you load this configuration file of tests, with an initializer class defined by Spring to load application. yml file.


1 Answers

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.

like image 73
g00glen00b Avatar answered Oct 06 '22 01:10

g00glen00b