最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring AOP的實(shí)現(xiàn)原理詳解及實(shí)例

 更新時(shí)間:2017年05月17日 09:01:44   作者:夢(mèng)想漲價(jià)了  
這篇文章主要介紹了Spring AOP的實(shí)現(xiàn)原理詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下

Spring AOP的實(shí)現(xiàn)原理詳解及實(shí)例

spring 實(shí)現(xiàn)AOP是依賴JDK動(dòng)態(tài)代理和CGLIB代理實(shí)現(xiàn)的。

以下是JDK動(dòng)態(tài)代理和CGLIB代理簡(jiǎn)單介紹

    JDK動(dòng)態(tài)代理:其代理對(duì)象必須是某個(gè)接口的實(shí)現(xiàn),它是通過(guò)在運(yùn)行期間創(chuàng)建一個(gè)接口的實(shí)現(xiàn)類來(lái)完成對(duì)目標(biāo)對(duì)象的代理。

    CGLIB代理:實(shí)現(xiàn)原理類似于JDK動(dòng)態(tài)代理,只是它在運(yùn)行期間生成的代理對(duì)象是針對(duì)目標(biāo)類擴(kuò)展的子類。CGLIB是高效的代碼生成包,底層是依靠ASM(開(kāi)源的Java字節(jié)碼編輯類庫(kù))操作字節(jié)碼實(shí)現(xiàn)的,性能比JDK強(qiáng)。  

在Spring中,有接口時(shí)將采用JDK的方式實(shí)現(xiàn)proxy代理對(duì)象,當(dāng)沒(méi)有接口時(shí),將采用cglib中的方式實(shí)現(xiàn)prixy代理對(duì)象。詳情如下:

// JDK方式:PersonService為接口,PersonServiceBean為實(shí)現(xiàn)類, 
 
 public class JDKProxyFactory implements InvocationHandler { 
  private Object targetObject; 
   
  public Object createProxyIntance(Object targetObject) 
  { 
  this.targetObject=targetObject; 
  return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),  
   this.targetObject.getClass().getInterfaces(), this); 
  } 
 
public Object invoke(Object proxy, Method method, Object[] args) 
 throws Throwable { 
  PersonServiceBean person=(PersonServiceBean)this.targetObject; 
  Object result=null; 
   if(person.getUser()!=null) 
   {  
   result = method.invoke(targetObject, args); 
   } 
  return result; 
} 
} 
//使用CGlib包實(shí)現(xiàn):PersonServiceBean為實(shí)現(xiàn)類,  而沒(méi)有PersonService接口,      
 
public class CGlibProxyFactory implements MethodInterceptor{ 
 private Object targetObject; 
  
 public Object createProxyInstance(Object targetObject) 
 {  
  this.targetObject=targetObject; 
  Enhancer enhancer=new Enhancer(); 
  enhancer.setSuperclass(this.targetObject.getClass());//設(shè)置目標(biāo)類的子類,該子類會(huì)覆蓋所有父類中的非final方法 
  enhancer.setCallback(this);//設(shè)置回調(diào) 
 return enhancer.create(); 
 } 
 
public Object intercept(Object proxy, Method method, Object[] args, 
 MethodProxy methodProxy) throws Throwable { 
 PersonServiceBean person=(PersonServiceBean)this.targetObject; 
  Object result=null; 
   if(person.getUser()!=null) 
   {  
   result = methodProxy.invoke(targetObject, args); 
   } 
 return null; 
} 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

宣化县| 蕲春县| 益阳市| 南江县| 怀仁县| 宿州市| 从化市| 白城市| 莎车县| 房产| 小金县| 永定县| 团风县| 岗巴县| 资溪县| 炉霍县| 涪陵区| 清新县| 盐津县| 天门市| 邯郸县| 迁西县| 宁都县| 日土县| 白山市| 安远县| 城固县| 金溪县| 绵竹市| 乌拉特前旗| 榆社县| 武城县| 清丰县| 赤城县| 黔西| 凤凰县| 吴川市| 鄯善县| 东明县| 镇赉县| 同心县|