I'm writing a custom Spring Boot starter and need to check for certain requirements in the client application's context. I do this at @Conditional evaluation time using custom Condition classes. One of these conditions needs to check for the presence of a Postgres-connected DataSource. The problem is that I can't seem to find a way to ensure that the condition is evaluated after a DataSource is created, even though one will be available after a few seconds.
To ensure I have a viable context at @Conditional evaluation time, I used two chained auto-configuration classes: the first loads a "context validation bean," and the second performs the actual job. Like this:
@AutoConfiguration
@EnableConfigurationProperties(OutboxStarterProperties.class)
public class ContextInitAutoconfiguration {
@Bean
public ContextManager outboxContextRequirementValidator(
Environment environment,
ApplicationContext applicationContext
){
return new ContextManager(environment, applicationContext);
}
}
@AutoConfiguration(after = ContextInitAutoconfiguration.class)
public class OutboxStarterAutoconfiguration {
......
I tried both
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
and
@AutoConfiguration(after = DataSourceAutoConfiguration.class)
none of them worked.
@Beanthat requires aDataSource.@ConditionalOnBeanand@ConditionalOnMissingBean, but it would be more of a workaround. What I'd like to have is a single reliable entrypoint where I can be sure that the context is ready and then potentially check n conditions like: beans presence, configuration, and so on.