Skip to content

Conditional registration of @ConfigurationProperties bean #2312

Closed
@chrylis

Description

@chrylis

I have an outstanding SO question that I don't think is supported as a use case as of 1.2.1. Essentially, I would like to use the functionality provided by @ConditionalOnProperty along with the type-safe @ConfigurationProperties. Currently, Spring Boot registers the properties bean regardless of whether any of the properties listed on it exist, so it's impossible to use @ConditionalOnBean(MyConfigProps.class) (I just get a bean with all null fields).

I would like either a way to suppress registration of an "empty" properties bean or a condition that matches only if the bean of the specified @ConfigurationProperties class exists.

Example:

@ConfigurationProperties(prefix = 'my.service')
class ServiceProps {
    String apiKey
}

@Configuration
@ConditionalOnConfigurationProperties(ServiceProps.class)
// or @ConditionalOnBean(ServiceProps.class)
class MyServiceAutoConfig {
    @Autowired
     ServiceProps serviceProps

    @Bean MyService myService() // ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions