Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @Conditional Spring annotation without knowing the whole property name

I want to optionally enable a @Configuration, based on whether a type of application.properties property exists or not. However I will not know the whole property name in advance, only part of its prefix i.e.

spring.security.oauth2.<provider-name>.url=x.y.z

And the goal is to enable the configuration using something like:

@ConditionalOnProperty(prefix = "spring.security", value="oauth2.*")

or something like that but it doesn't seem to work. I've tried multiple combinations, none work it always expects the full property name but I don't know the <provider-name> in advance. Any ways to achieve this functionality?

like image 325
PentaKon Avatar asked Oct 20 '25 02:10

PentaKon


1 Answers

As of right now Spring does not support the requested functionality with the out of the box annotations which means a custom @Conditional condition must be created that does what we want. The Condition implementation looks like this:

public class EnableOAuth2Condition implements Condition {
  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Environment env = context.getEnvironment();
    for(Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) {
      PropertySource propertySource = (PropertySource) it.next();
      if (propertySource instanceof MapPropertySource) {
        for (String propertyName : ((MapPropertySource) propertySource).getSource().keySet()) {
          if(propertyName.startsWith("spring.security.oauth2")) {
            return true;
          }
        }
      }
    }
    return false;
  }
}

and the way to use it on a @Configuration class is the following:

@Configuration
@Conditional(EnableOAuth2Condition.class)
public class MyOAuth2Config {
  ...
}

A bit messy for something simple but that's the way to go for now.

like image 69
PentaKon Avatar answered Oct 22 '25 04:10

PentaKon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!