在代理类中引用动态代理
考慮一個InventoryService接口:
public interface InventoryService{public Inventory create(Inventory inventory);public List<Inventory> list();public Inventory findByVin(String vin);public Inventory update(Inventory inventory);public boolean delete(Long id);public Inventory compositeUpdateService(String vin, String newMake); }還請考慮該服務的默認實現,并假設最后一個方法CompositeUpdateService內部在Bean本身上調用兩個方法,如下所示:
public Inventory compositeUpdateService(String vin, String newMake) {logger.info("composite Update Service called");Inventory inventory = this.findByVin(vin);inventory.setMake(newMake);this.update(inventory);return inventory; }如果我現在創建一個方面來建議對InventoryService的任何調用,以跟蹤每個方法調用花費的時間,Spring AOP將為InventoryService bean創建一個動態代理:
但是,對CompositeUpdateService的調用將僅在此方法的級別上記錄時間,即CompositeUpdateService內部對findByVin的調用,update繞過了代理,因此不會被跟蹤:
一個好的解決方法是使用AspectJ的全部功能– AspectJ會更改DefaultInventoryService的所有方法的字節碼,以包括對建議的調用。
我們制定的解決方法是將對代理本身的引用注入到bean中,而不是調用this.findByVin和this.update,而是調用proxy.findByVin和proxy.update!
因此,現在我們如何將對代理的引用干凈地注入到bean中-我提供的解決方案是創建一個接口,以標記對自己的代理感興趣的bean:
public interface ProxyAware<T> {void setProxy(T proxy); }感興趣的接口及其實現如下所示:
public interface InventoryService extends ProxyAware<InventoryService>{ ... }public class DefaultInventoryService implements InventoryService{ ...private InventoryService proxy;@Overridepublic void setProxy(InventoryService proxy) {this.proxy = proxy;} }然后定義一個BeanPostProcessor注入這個代理!
public class ProxyInjectingBeanPostProcessor implements BeanPostProcessor, Ordered {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (AopUtils.isAopProxy((bean))){try {Object target = ((Advised)bean).getTargetSource().getTarget();if (target instanceof ProxyAware){((ProxyAware) target).setProxy(bean);}} catch (Exception e) {return bean;}}return bean;}@Overridepublic int getOrder() {return Integer.MAX_VALUE;} }不是最干凈的實現,但是可以!
參考: http : //blog.springsource.org/2012/05/23/understanding-proxy-usage-in-spring/
參考: all和雜項博客中, JCG合作伙伴 Biju Kunjummen 引用了代理課程中的動態代理 。
翻譯自: https://www.javacodegeeks.com/2012/07/reference-to-dynamic-proxy-in-proxied.html
總結
以上是生活随笔為你收集整理的在代理类中引用动态代理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓天天炫斗下架了吗(安卓天天炫斗)
- 下一篇: 骑士小组1亿(骑士小组 ddos)