I am developing a new feature that should be hidden behind a FeatureToggleX
. As part of that, I need to add a new dependency b
to my SomeClass
:
class SomeClass {
private OldDependency a;
private NewDependency b;
@Autowired
public SomeClass(OldDependency a, NewDependency b) {
this.a = a;
this.b = b;
}
}
I use a custom IfActive
annotation as Conditional
that serves TheToggledDependency
if, and only if, FeatureToggleX
is active:
@Component
@IfActive("FeatureToggleX")
class TheToggledDependency implements NewDependency {
}
How can I make Spring temporarily wire b = null
while FeatureToggleX
is off?
Is there a clean way to plug a generic solution into Spring's wiring process that detects IfActive
components and supplies null
s in their places?
As recommended in other answers, I considered
@Autowired(required = false)
- maybe in combination with overloadingSomeClass
's constructor - to allow omittingb
, but this has the side effect that it also legalizes omittinga
, which should be an error.I could provide an alternative dummy
IfInactive
variant ofNewDependency
, but that would fill my project with worthless dummies that I need to remember removing when the toggle is removed.Similarly, I could build two variants of
SomeClass
, one that hasNewDependency
and a second that is completely unaware of it, and choose between the two depending on the toggle. However, that is impractical for larger classes because it duplicates other, unaffected functionality.