<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=david_syer" rel="nofollow">Dave Syer</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-2804?redirect=false" rel="nofollow">SPR-2804</a></strong> and commented</p>
<p dir="auto">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...</p>
<p dir="auto">1.2. Target Chains</p>
<p dir="auto">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:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="*      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."><pre class="notranslate"><code class="notranslate">*      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.
</code></pre></div>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">1.2.1. Example of Target Chain</p>
<p dir="auto">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).</p>
<p dir="auto">&lt;bean id="compositeResourceLocator"<br>
class="org.springmodules.aop.framework.TargetChainProxyFactoryBean"&gt;<br>
&lt;property name="objectType" value="com.mycompany.ResourceLocator"/&gt;</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;property name=&quot;target&quot;&gt;
	&lt;list&gt;
		&lt;ref bean=&quot;localResourceLocator&quot;/&gt;
		&lt;ref bean=&quot;remoteResourceLocator&quot;/&gt;
	&lt;/list&gt;
&lt;/property&gt;"><pre class="notranslate"><code class="notranslate">&lt;property name="target"&gt;
	&lt;list&gt;
		&lt;ref bean="localResourceLocator"/&gt;
		&lt;ref bean="remoteResourceLocator"/&gt;
	&lt;/list&gt;
&lt;/property&gt;
</code></pre></div>
<p dir="auto">&lt;/bean&gt;<br>
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.</p>
<hr>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398063305" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/6382" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/6382/hovercard" href="https://github.com/spring-projects/spring-framework/issues/6382">#6382</a> ChainOfResponsibilityFactoryBean; factoryBean which creates a proxy that acts as a COR over a list of objects (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=yatesco" rel="nofollow">Colin Yates</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-1684?redirect=false" rel="nofollow">SPR-1684</a></strong> and commented</p>
<p dir="auto"><a href="http://forum.springframework.org/showthread.php?t=21986" rel="nofollow">http://forum.springframework.org/showthread.php?t=21986</a></p>
<p dir="auto">I keep writing a number of implementations of a single interface and then needing to wrap all those implementations so they are all called.</p>
<p dir="auto">For example, I might have an ErrorHandler interface and a Log4J implementation, then a Mailing implementation etc. and I want them all to fire when invoked.  I would then write a Composite implementation which traversed them all.  This was tiresome so I decided to write a FactoryBean which would do the same thing.</p>
<p dir="auto">Attached is the source code.  It may be helpful to others, it might not :)</p>
<hr>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/11458/Copy+of+spring-utils.zip" rel="nofollow">Copy of spring-utils.zip</a> (<em>2.55 MB</em>)</li>
<li><a href="https://jira.spring.io/secure/attachment/11457/cor.zip" rel="nofollow">cor.zip</a> (<em>2.27 MB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398072544" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/7491" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/7491/hovercard" href="https://github.com/spring-projects/spring-framework/issues/7491">#7491</a> Target Chains: factory bean for a proxy that builds up a composite service from a list of targets (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>
<p dir="auto">4 votes, 3 watchers</p>