You can use the bean post processor to set the proxy server link in the target bean. It moves Spring specifics from your beans to one class.
postprocessor
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class SelfReferencingBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof SelfReferencingBean) { ((SelfReferencingBean) bean).setProxy(bean); } return bean; } }
Context
Register the postprocessor in applicationContext.xml .
<bean id="srbpp" class="SelfReferencingBeanPostProcessor"/>
Beans
Each bean must implement a SelfReferencingBean in order to tell the post processor that it needs a proxy link.
public interface SelfReferencingBean { void setProxy(Object proxy) ; }
Now we implement setProxy in each bean, which must call itself through its proxy.
public class MyBean implements SelfReferencingBean { MyBean proxy; @Override public void setProxy(Object proxy) { this.proxy = (MyBean) proxy; } }
You can put this last bit of code into the bean base class if you don't mind entering proxy in the bean type when calling methods directly on it. Since you are going through Method.invoke , you donβt even need to throw.
With a little work, I'm sure it could be converted to an a @Autowired annotation handler. Think about it, I donβt remember, even if I tried to add self-esteem using @Autowired .
public class MyBean implements SelfReferencingBean { @Autowired MyBean proxy; }
David Harkness Jun 30 2018-12-12T00: 00Z
source share