I am trying to create a custom condition using spring context.
- I am using spring-context 5.3.13 (no spring-boot)
- I have a file named "config.yaml" under "src/main/resources"
- I have this @PropertySource custom configured: @PropertySource(value = "classpath:config.yaml", factory = TypesafeAdapterPropertySourceFactory.class)
- I am trying to get a property from inside a custom spring Condition:
public class MyConditionalOnProperty implements ConfigurationCondition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// just to test what property sources are present
for(Iterator it = ((AbstractEnvironment) context.getEnvironment()).getPropertySources().iterator(); it.hasNext(); ) {
PropertySource propertySource = (PropertySource) it.next();
System.out.println(propertySource.getName());
//prints only "systemProperties" and "systemEnvironment"
}
return context.getEnvironment().containsProperty("my.prop");
}
@Override
public ConfigurationPhase getConfigurationPhase() {
return ConfigurationPhase.REGISTER_BEAN;
}
}
- my.prop is present after the application context was loaded
- I have a bean annotated with the condition:
@Named
@Singleton
@Conditional(PropertyEnabledCondition.class)
public class MyTestBean {
}
I have also debugged and found that my custom property source are loaded only after this stage.
Is there a way to retrieve application properties in condition or recreate @ConditionalOnProperty without using spring-boot some other way?