question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional registration of @ConfigurationProperties bean

See original GitHub issue

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() // ...
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
philwebbcommented, May 23, 2018

Thanks @izeye. I’ve changed the tag.

1reaction
chryliscommented, Jan 12, 2015

Sort of, except that if the properties are present, I don’t understand the purpose of a separate enable property; it seems like duplication to me. The use case is the exact same as for @ConditionalOnProperty, just wanting to make use of the type-safe config object instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot bean conditional on @ConfigurationProperties ...
Spring Boot gives us typed configuration objects using the @ConfigurationProperties annotation. One of the advantages is getting property ...
Read more >
Conditional Beans with Spring Boot - Reflectoring
It allows to load beans conditionally depending on a certain environment property: @Configuration @ConditionalOnProperty( value="module.
Read more >
The Spring @ConditionalOnProperty Annotation - Baeldung
In short, the @ConditionalOnProperty enables bean registration only if an environment property is present and has a specific value. By default, ...
Read more >
36. Developing auto-configuration and using conditions - Spring
The @ConditionalOnMissingBean is one common example that is used to allow developers to 'override' auto-configuration if they are not happy with your defaults. ......
Read more >
Spring - @Bean conditional registration - LogicBig
The purpose of this annotation is to enable/disable target beans from being registered based on some condition. These conditions are applied ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found