Skip to content

Conditional registration of @ConfigurationProperties bean #2312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrylis opened this issue Jan 8, 2015 · 15 comments
Closed

Conditional registration of @ConfigurationProperties bean #2312

chrylis opened this issue Jan 8, 2015 · 15 comments
Labels
status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@chrylis
Copy link
Contributor

chrylis commented Jan 8, 2015

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() // ...
}
@chrylis
Copy link
Contributor Author

chrylis commented Jan 8, 2015

This is tangentially related to #2282.

@dsyer
Copy link
Member

dsyer commented Jan 8, 2015

Can't you do this:

@Bean
@ConfigurationProperties('my.service')
@ConditionalOnWhatever
public ServiceProps serviceProps() { return new ServiceProps(); }

@philwebb philwebb added the type: enhancement A general enhancement label Jan 8, 2015
@chrylis
Copy link
Contributor Author

chrylis commented Jan 9, 2015

The issue is specifically that @Conditional. It would have to duplicate the information (e.g., list of property names) inside ServiceProps. I'd like to have a standard condition that matches if/unless a typed @ConfigurationProperties gets populated instead of having to list the property names in the annotation.

@dsyer
Copy link
Member

dsyer commented Jan 10, 2015

I'm not sure I understand the use case yet. We often use a single "enabled" flag to switch a feature on. Is that the sort of thing?

@chrylis
Copy link
Contributor Author

chrylis commented 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.

@snicoll
Copy link
Member

snicoll commented Jan 12, 2015

I think that #2282 is a better take at this issue.

If I understand what you're saying, the @ConfigurationProperties annotated pojo should be "injected" only if (at least) one of its property is set. And then use that to enable a behaviour in auto-configuration. I think it's a bit vague to say that "if one property of that pojo is set then do X" and I really like the explicit "enabled" flag as it reads quite well in auto-configuration.

The other problem is when you want to disable the auto-config. Let's assume that you have configured tons of properties for that POJO. With your system, you'll have to comment these out instead of just adding a xyz.enabled=false property. Assume now you have defined those properties in a separate configuration file that is enabled by a profile or something. Now you have to touch that file to disable the behaviour.

My point is that the separate enabled property is extremely useful. If we can agree on that, then #2282 is actually a duplicate of this issue. Does that make sense?

@chrylis
Copy link
Contributor Author

chrylis commented Jan 13, 2015

I can see the multiple-properties-merge issue, and looking at the enabled flag as perhaps more useful as a "disabled" flag makes sense.

What about a @Condition something like this:

Class<@ConfigurationProperties> value;
String enableProperty default "enabled";

(I think that #2282 and this one are both distinct flawed approaches at the same general goal, which seems to be something like this.)

@snicoll
Copy link
Member

snicoll commented Jan 13, 2015

I am sorry, I didn't get your example.

@chrylis
Copy link
Contributor Author

chrylis commented Jan 13, 2015

I mean an @Conditional annotation with properties something like that, so that by default it will check the enabled property on an @ConfigurationProperties object (so no configuration required for the usual case of prefix.enabled).

@snicoll
Copy link
Member

snicoll commented Jan 14, 2015

Oh, interesting.

@longfeizheng
Copy link

longfeizheng commented Jan 10, 2018

@Configuration
@EnableConfigurationProperties(ServiceProps .class)
public class CoreConfig {

}

@philwebb
Copy link
Member

We're cleaning out the issue tracker and closing issues that we've not seen much demand to fix. Feel free to comment with additional justifications if you feel that this one should not have been closed.

@philwebb philwebb added the status: duplicate A duplicate of another issue label Mar 22, 2018
@izeye
Copy link
Contributor

izeye commented May 23, 2018

This looks intended to tag as "declined" based on the @philwebb 's comment.

@philwebb philwebb added status: declined A suggestion or change that we don't feel we should currently apply and removed status: duplicate A duplicate of another issue labels May 23, 2018
@philwebb
Copy link
Member

Thanks @izeye. I've changed the tag.

@tokrug
Copy link

tokrug commented Dec 14, 2020

For anyone interested in a flexible workaround check my comment in another issue. It's a full implementation of @ConditionalOnConfigurationProperties that you can use in your project. It has some downsides but works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

7 participants