How do I override a Spring bean definition yet still reference the overridden bean?

后端 未结 5 848
忘了有多久
忘了有多久 2020-12-29 13:03

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 imp

5条回答
  •  [愿得一人]
    2020-12-29 13:52

    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 service bean 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 two service beans, and without modifying the original service bean, is to use a Spring AutoProxyCreator, probably a BeanNameAutoProxyCreator.

    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 service as before, but with your additional functionality.

提交回复
热议问题