I’m attempting to implement a delegate Service provider by overriding the bean definition for the original service with my delegate Service. However, as the name would imply, the delegate Service needs a reference to the original service to delegate calls to.
I’m having trouble figuring out how to override the bean definition while using the original bean def without running into a circular reference issue.
For example:
<!-- Original service def in spring-context.xml -->
<bean id="service" class="com.mycompany.Service"/>
<!-- Overridden definition in spring-plugin-context.xml -->
<bean id="service" class="com.mycompany.DelegatedService"/>
<constructor-arg ref="service"/>
</bean>
Is this possible?
The short answer to your question is that you cannot have two bean definitions with the same name. If you try, one will hide the other, and only one definition will be usable.
Your question’s example seems to suggest that you’re trying to wrap the original
servicebean in a proxy object, with the wrapper performing some before-and-after work around calls to the service. One way to achieve this, without defining twoservicebeans, and without modifying the originalservicebean, is to use a SpringAutoProxyCreator, probably aBeanNameAutoProxyCreator.This allows you to list a bean (or beans) that are to be automatically proxied. You specify the interceptors you want to be applied to invocations on the target bean. You would implement these interceptors to do the work you need to do.
Spring would automatically create a delegating proxy for you, which would have the bean id
serviceas before, but with your additional functionality.