Monday, December 25, 2017

Using EmbeddedValueResolver with @Conditional

Leave a Comment

Problem context: in our application we use custom PropertySourcesPlaceholderConfigurer to extend possibilities of EmbeddedValueResolver with custom property sources. I am very inspired of Spring Boot @ConditionalOnProperty and @ConditionalOnExpression and want to use it with custom placeholders.

What I wanted to do was to write custom @Conditional annotation that uses EmbeddedValueResolver to resolve placeholders (${...}). I noticed that ConfigurationClassPostProcessor implements ResourceLoaderAware interface, and ResourceLoaderAware classes are configured later than EmbeddedValueResolverAware ones. So when we initialize ResourceLoaderAware, our EmbeddedValueResolverAware beans are already configured. So potentially solution seems possible.

What I wanted to do next was to write custom Condition that uses EmbeddedValueResolver from ApplicationContext to resolve some property and match it to some value. But the problem is that Condition uses some another context class called ConditionContext which doesn't include EmbeddedValueResolver. Using Environment#resolvePlaceholder doesn't help - seems like Environment doesn't know anything about placeholders that are available through EmbeddedValueResolver.

So, is that possible to use EmbeddedValueResolver with Spring conditionals?

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment