We have a large number of spring beans that are neatly wrapped by specific *Context.xml files. When unit testing, we only want to load specific contexts for given tests - the loading of some subsystems is unnecessarily expensive for all tests. This is all fine & done, but we need fine-grained control over the shutdown of these subsystems (eveybody listening on ContextClosedEvent is too unorchestrated) and want to do the the following:

<bean class="generalservices.util.ShutdownHandler">
<constructor-arg>
<util:list>
<ref bean="subsystemOne"/>
<ref bean="subsystemTwo"/>
<ref bean="subsystemThree"/>
</util:list>
</constructor-arg>
</bean>

Now, we want to define/load this bean once, irrespective of whether a particular subsystem has been loaded or not.


There are many "other" solutions that would fix this - like refactoring our Spring context initialisation strategy or getting each subsystem to "register" themselves, but I'm wondering whether we can annotate these references to be robust to non-existent beans.

TIA