日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring AOP策略模式使用

發(fā)布時間:2025/4/5 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring AOP策略模式使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.策略模式

The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it

          策略模式UML圖

?

2.策略模式組成

—抽象策略角色: 策略類,通常由一個接口或者抽象類實現(xiàn)。 —具體策略角色:包裝了相關(guān)的算法和行為。 —環(huán)境角色:持有一個策略類的引用,最終給客戶端調(diào)用。 3.spring AOP策略模式使用 分析角色 —抽象策略角色--AopProxy —具體策略角色--Cglib2AopProx和JdkDynamicAopProxy —環(huán)境角色--ProxyCreatorSupport 調(diào)用方式:ProxyFactoryBean.java 1 /** 2 * Return the singleton instance of this class's proxy object, 3 * lazily creating it if it hasn't been created already. 4 * @return the shared singleton proxy 5 */ 6 private synchronized Object getSingletonInstance() { 7 if (this.singletonInstance == null) { 8 this.targetSource = freshTargetSource(); 9 if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) { 10 // Rely on AOP infrastructure to tell us what interfaces to proxy. 11 Class targetClass = getTargetClass(); 12 if (targetClass == null) { 13 throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy"); 14 } 15 setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader)); 16 } 17 // Initialize the shared singleton instance. 18 super.setFrozen(this.freezeProxy); 19 this.singletonInstance = getProxy(createAopProxy()); 20 } 21 return this.singletonInstance; 22 }

真實代碼實現(xiàn)在DefaultAopProxyFactory.java

1 public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { 2 if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { 3 Class targetClass = config.getTargetClass(); 4 if (targetClass == null) { 5 throw new AopConfigException("TargetSource cannot determine target class: " + 6 "Either an interface or a target is required for proxy creation."); 7 } 8 if (targetClass.isInterface()) { 9 return new JdkDynamicAopProxy(config); 10 } 11 if (!cglibAvailable) { 12 throw new AopConfigException( 13 "Cannot proxy target class because CGLIB2 is not available. " + 14 "Add CGLIB to the class path or specify proxy interfaces."); 15 } 16 return CglibProxyFactory.createCglibProxy(config); 17 } 18 else { 19 return new JdkDynamicAopProxy(config); 20 } 21 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/archive/2013/03/21/2973853.html

總結(jié)

以上是生活随笔為你收集整理的spring AOP策略模式使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。