婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > Spring AOP的實(shí)現(xiàn)原理詳解及實(shí)例

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

熱門(mén)標(biāo)簽:好操作的電話機(jī)器人廠家 地圖標(biāo)注市場(chǎng)怎么樣 泰州泰興400電話 怎么申請(qǐng) 南京新思維電話機(jī)器人 聊城智能電銷(xiāo)機(jī)器人外呼 如何用中國(guó)地圖標(biāo)注數(shù)字點(diǎn) 企業(yè)怎么在聯(lián)通申請(qǐng)400電話 南昌市地圖標(biāo)注app 百度地圖添加標(biāo)注圖標(biāo)樣式

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ì)本站的支持!

您可能感興趣的文章:
  • 深入淺析Spring 的aop實(shí)現(xiàn)原理
  • 深入理解spring的AOP機(jī)制原理
  • Spring IOC和aop的原理及實(shí)例詳解
  • Spring AOP面向切面編程實(shí)現(xiàn)原理方法詳解
  • Spring AOP實(shí)現(xiàn)原理解析
  • Spring AOP注解案例及基本原理詳解
  • Spring AspectJ AOP框架注解原理解析
  • SpringAOP切點(diǎn)函數(shù)實(shí)現(xiàn)原理詳解
  • Spring Aop基本流程原理示例詳解

標(biāo)簽:烏蘭察布 臨汾 白銀 開(kāi)封 自貢 吉林 銅川 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Spring AOP的實(shí)現(xiàn)原理詳解及實(shí)例》,本文關(guān)鍵詞  Spring,AOP,的,實(shí)現(xiàn),原理,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Spring AOP的實(shí)現(xiàn)原理詳解及實(shí)例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Spring AOP的實(shí)現(xiàn)原理詳解及實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 泽普县| 扶沟县| 南川市| 宣化县| 通渭县| 博罗县| 哈巴河县| 西丰县| 塔河县| 榆树市| 湖北省| 紫云| 龙川县| 大同县| 汾阳市| 宁安市| 通化市| 庄河市| 中方县| 翼城县| 沙雅县| 张家港市| 印江| 本溪市| 马公市| 长岛县| 汕尾市| 淅川县| 淄博市| 南川市| 陇西县| 汤阴县| 盐亭县| 宣恩县| 马关县| 开原市| 康定县| 轮台县| 巫山县| 红河县| 广南县|