-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
Dave Syer opened SPR-2804 and commented
Here are some docos I wrote for an implementation that I was considering for springmodules. It seems more like a core feature, when I think about it, so I thought I'd see what people think here...
1.2. Target Chains
Use the factory bean to create a composite service that delegates to a chain of existing services, all with the same interface. The chain is terminated according to some simple rules depending on the return type of the method being called. Thus:
* If the method returns Void, then all targets are executed.
* If the method returns Boolean (or boolean), then execution stops with the first target that returns true.
* If the method returns Object, then execution stops with the first target that returns not null.
Once the chain has finished executing the composite method returns the object returned by the last executed target. A Target Chain can itself take part in a chain as a target, and the behaviour remains the same.
If any part of the chain throws an exception the chain terminates and the exception is re-thrown by the composite. No clean up is attempted.
1.2.1. Example of Target Chain
To configure a Target Chain all that is needed is a chain of targets (a List), and an object type (which must be an Interface).
<bean id="compositeResourceLocator"
class="org.springmodules.aop.framework.TargetChainProxyFactoryBean">
<property name="objectType" value="com.mycompany.ResourceLocator"/>
<property name="target">
<list>
<ref bean="localResourceLocator"/>
<ref bean="remoteResourceLocator"/>
</list>
</property>
</bean>
This exposes a bean called compositeResourceLocator which implements the interface com.mycompany.ResourceLocator. Calls to (e.g.) a method URL locate(String) will be delegated first to the localResourceLocator, and if nothing is found (null returned), it will move on to the remoteResourceLocator.
Issue Links:
- ChainOfResponsibilityFactoryBean; factoryBean which creates a proxy that acts as a COR over a list of objects [SPR-1684] #6382 ChainOfResponsibilityFactoryBean; factoryBean which creates a proxy that acts as a COR over a list of objects ("duplicates")