Say I have:
interface A { ... }
class A1 implements A { ... }
class A2 implements A { ... }
@Lazy
@Configuration
class SpringConfig {
@Bean
A a1(DepA11 depA11, DepA12 depA12, ...) {
}
@Bean
A a2(DepA21 depA21, DepA22 depA22, ...) {
}
}
Now let's say there's some complex logic that depends on some injected dependencies that returns an int
:
int choose(DepChoose1 depChoose1, DepChoose2 depChoose2, ...) {
// Complex logic that depends on depChoose1, depChoose2, ...
int res = ...;
return res;
}
and that I want Spring to autowire either a1
or a2
based on the return value.
It is imperative not to instantiate the other bean (or beans in general - we can have a3
, a4
, ...), as each of them incur heavy processing on startup and also can have side-effects that have to be avoided if the other bean is to be chosen initially.
A1
and A2
are stream-like sources of items for other parts of the system to consume. Some of their (not immediate) dependencies have initialization via @PostConstruct
.
Their dependencies are also push-based. Whatever they fetch, they push to B
which then forwards to other consumers. So just initializing them would create unwanted pushes.
I thought about using @Conditional
, but it inherently doesn't support dependencies.
Is there a succinct way to do this in Spring?