0

I have the following interface :

@Primary
@ConditionalOnProperty(value = "ms.couchbase.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(value = "iiams.switch.iiamsSingleProcessIdPer100", havingValue = "true", matchIfMissing = true)
public interface AsyncCommonRepository extends CouchbaseRepository<CouchbaseDocument, Long> { }

Of course this does not compile.
All I want is to combine somehow the two @ConditionalOnProperty. Should I use @ConditionalOnExpression?
Thanks in advance!

  • Does this answer your question? [How to check two condition while using @ConditionalOnProperty or @ConditionalOnExpression](https://stackoverflow.com/questions/36016864/how-to-check-two-condition-while-using-conditionalonproperty-or-conditionalone) – Tuan Pham May 25 '22 at 09:27
  • Actually no, I don't know how to use havingValue and matchIfMissing – Miltos Dimitriadis May 30 '22 at 11:37

1 Answers1

2

You can use AllNestedConditions to combine two or more conditions:

class AsyncCommonRepositoryCondition extends AllNestedConditions {

    AsyncCommonRepositoryCondition() {
        super(ConfigurationPhase.PARSE_CONFIGURATION);
    }

    @ConditionalOnProperty(value = "ms.couchbase.enabled", havingValue = "true", matchIfMissing = true)
    static class CouchbaseEnabled {

    }

    @ConditionalOnProperty(value = "iiams.switch.iiamsSingleProcessIdPer100", havingValue = "true", matchIfMissing = true)
    static class SingleProcessIdPer100 {

    }

}

This condition can then be used on your repository:

@Primary
@Conditional(AsyncCommonRepositoryCondition.class)
public interface AsyncCommonRepository extends CouchbaseRepository<CouchbaseDocument, Long> { }
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242