Affects: 5.3.23
In working to decouple a monolithic application using Spring Integration, I ran into a chicken-and-egg problem where @Bean
methods can indirectly provide beans that are not "visible" from their method signatures—in particular, an IntegrationFlow
bean may register beans that are interface proxies for gateways, but the Spring context (both the regular autowiring and Boot auto-configuration) are not aware that these beans will be present, and so NoSuchBeanDefinitionException
can occur at launch.
For such cases, and as configuration DSLs get progressively more powerful but still contained inside the method body, it would be helpful to have an @ProvidesBean
annotation that could be added to @Bean
methods to promise to the context that certain beans will be made available.
@Target({METHOD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Repeatable
public @interface ProvidesBean {
Class<?> value();
String[] name() default {};
// qualifiers
// ..
}
@Configuration
class MyConfig {
@Bean
@ProvidesBean(MyGateway.class)
IntegrationFlow springIntegration() {
IntegrationFlows.from(MyGateway.class).channel(GATEWAY_CHANNEL).get()
}
}
This could create something like a BeanPromise
bean definition, ideally with an error message in the case of failure to actually provide the bean indicating where the broken promise was made.
Comment From: bclozel
Closing as there is not much demand for this and this approach would not work with the new AOT infrastructure.
Comment From: vpavic
Regarding the demand part, I'd be very interested to see this and for exactly the same scenario that @chrylis outlined.
Workarounds (@Lazy
or @DependsOn
) are not optimal each for its own reasons.