Skip to content

Need the ability to conditional add configuration when a property is missing. #4938

Closed
@tkvangorder

Description

@tkvangorder

I have a use case where I only want to configure beans if and ONLY if a given property is NOT present within the environment.

I was using @ConditionalOnProperty and setting matchOnMissing = true. And this condition evaluates to "true" when the property is missing OR the property is present and has any value other than "false".

I either need a new annotation @ConditionalOnMissingProperty or a new configuration option added to @ConditionalOnProperty that allows "only match if not present"

I was able to work around this issue with @ConditonalOnExpression but this ended up being quite tricky because the property I was testing for was an MSSQL server jdbc url....which had "" in it...

The parser was replacing my variable with the jdbc URL and then re-evaluating the expression which ended up with a syntax error on the backslash. The solution was to enclose my environment property in single quotes to get around this parsing error but then I am evaluating string literals. This ended up being a lot more complicated than it should have been.

so if the property is absent, I end up with a string literal "true"....ugh.

    @Configuration
    @ConditionalOnExpression("'${datasource.reporter.url:true}' == 'true'")
    protected static class AliasReporterDataSourceConfig {

If you have a preference on an approach I could take a stab at a pull request for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions