日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Spring引介增強IntroductionAdvisor使用

環(huán)境:Spring5.2.14

創(chuàng)新互聯(lián)建站主營鄂州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā),鄂州h5微信小程序開發(fā)搭建,鄂州網(wǎng)站營銷推廣歡迎鄂州等地區(qū)企業(yè)咨詢

在不修改業(yè)務(wù)代碼的情況下如何讓某個類具有某項功能呢,比如具有XXXDAO接口的能力?

1 IntroductionAdvisor介紹

IntroductionAdvisor與PointcutAdvisor區(qū)別

  1. IntroductionAdvisor只能應(yīng)用于類級別
  2. IntroductionAdvisor只能應(yīng)用于Introduction類型的通知,而PointcutAdvisor可以應(yīng)用于所有類型的通知
  3. IntroductionAdvisor的Advice需要實現(xiàn)目標的接口,而pintcutAdvisor中的Advice沒有改要求

2 IntroductionAdvisor使用流程

假定我們的業(yè)務(wù)類CustomDAO希望具有DesignDAO(接口)的能力

2.1 目標接口

 
 
 
  1. public interface DesignDAO { 
  2.      
  3.     public void design() ; 
  4.      

2.2 Introduction攔截器

 
 
 
  1. public class CustomIntroductionInterceptor implements IntroductionInterceptor, DesignDAO { 
  2.     // 判斷當(dāng)前的攔截器是否實現(xiàn)了目標接口(DesignDAO,我們需要某個類具有指定接口的功能) 
  3.     @Override 
  4.     public boolean implementsInterface(Class intf) { 
  5.         return intf.isAssignableFrom(this.getClass()) ; 
  6.     } 
  7.  
  8.     @Override 
  9.     public Object invoke(MethodInvocation invocation) throws Throwable { 
  10.         System.out.println("我是通知類:IntroductionInterceptor") ; 
  11.         // 判斷當(dāng)前執(zhí)行的方法所屬類是否實現(xiàn)了目標接口 
  12.         // 這里必須要進行相應(yīng)的判斷攔截,否則會在沒有被攔截的類方法執(zhí)行的時候報錯我 
  13.         // 因為你的其它類所對應(yīng)的接口并沒有在該攔截器中被實現(xiàn)。 
  14.         if (implementsInterface(invocation.getMethod().getDeclaringClass())) { 
  15.             return invocation.getMethod().invoke(this, invocation.getArguments()) ; 
  16.         } 
  17.         return invocation.proceed() ; 
  18.     } 
  19.  
  20.     @Override 
  21.     public void design() { 
  22.         System.out.println("接口實現(xiàn)了") ; 
  23.     } 
  24.          

2.3 IntroductionAdvisor定義

 
 
 
  1. @Component 
  2. public class CustomIntroductionAdvisor implements IntroductionAdvisor { 
  3.      
  4.     // 定義Advice通知 
  5.     @Override 
  6.     public Advice getAdvice() { 
  7.         return new CustomIntroductionInterceptor() ; 
  8.     } 
  9.  
  10.     // 該方法沒有被使用,建議直接返回true 
  11.     @Override 
  12.     public boolean isPerInstance() { 
  13.         return true ; 
  14.     } 
  15.  
  16.     // 定義了所要實現(xiàn)的所有接口 
  17.     @Override 
  18.     public Class[] getInterfaces() { 
  19.         return new Class[] {DesignDAO.class} ; 
  20.     } 
  21.  
  22.     // 過濾類,返回true就會被匹配 
  23.     @Override 
  24.     public ClassFilter getClassFilter() { 
  25.         return new ClassFilter() { 
  26.             @Override 
  27.             public boolean matches(Class clazz) { 
  28.                 return CustomDAO.class.isAssignableFrom(clazz) ; 
  29.             } 
  30.         } ; 
  31.     } 
  32.  
  33.     // 在這里我們可以校驗我們的Advice類是否實現(xiàn)了執(zhí)行的接口getInterfaces中定義的接口 
  34.     @Override 
  35.     public void validateInterfaces() throws IllegalArgumentException { 
  36.         // 這里可以參考DefaultIntroductionAdvisor的實現(xiàn) 
  37.     } 
  38.  

這里可以查看示例文檔37示例源碼是如何執(zhí)行的

2.4 驗證

 
 
 
  1. @Component 
  2. public class CustomerDAOImpl implements CustomDAO { 
  3.          
  4.     public void update() { 
  5.         System.out.println("更新數(shù)據(jù)..." + this.getClass()) ; 
  6.     } 
  7.  
  8.     public void save() { 
  9.         System.out.println("保存方法..." + this.getClass()) ; 
  10.     } 
  11.      

 業(yè)務(wù)類并沒有實現(xiàn)DesignDAO接口。接下來看看通過上面定義IntroductionAdvisor是否可以讓我們的業(yè)務(wù)類具有目標類的功能

 
 
 
  1. public class LauncherMain { 
  2.      
  3.     public static void main(String[] args) { 
  4.         System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true") ; 
  5.         AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.pack") ; 
  6.         CustomDAO dao = ctx.getBean(CustomDAO.class) ; 
  7.         System.out.println(dao.getClass()) ; 
  8.         System.out.println(Arrays.toString(dao.getClass().getInterfaces())) ; 
  9.         dao.save() ; 
  10.         dao.update() ; 
  11.         if (DesignDAO.class.isAssignableFrom(dao.getClass())) { 
  12.             DesignDAO ddao = (DesignDAO) dao ; 
  13.             System.out.println("IntroductionAdvisor start...") ; 
  14.             ddao.design() ; 
  15.             System.out.println("IntroductionAdvisor end...") ; 
  16.         } 
  17.         ctx.close() ; 
  18.     } 
  19.      

 執(zhí)行結(jié)果:

 
 
 
  1. class com.sun.proxy.$Proxy14 
  2. [interface com.pack.dao.CustomDAO, interface com.pack.interfaces.DesignDAO, interface org.springframework.aop.SpringProxy, interface org.springframework.aop.framework.Advised, interface org.springframework.core.DecoratingProxy] 
  3. 我是通知類:IntroductionInterceptor, interface com.pack.dao.CustomDAO 
  4. intf: interface com.pack.dao.CustomDAO 
  5. 我被調(diào)用了... 
  6. 保存方法...class com.pack.dao.CustomerDAOImpl 
  7. 我是通知類:IntroductionInterceptor, interface com.pack.dao.CustomDAO 
  8. intf: interface com.pack.dao.CustomDAO 
  9. 更新數(shù)據(jù)...class com.pack.dao.CustomerDAOImpl 
  10. IntroductionAdvisor start... 
  11. 我是通知類:IntroductionInterceptor, interface com.pack.interfaces.DesignDAO 
  12. intf: interface com.pack.interfaces.DesignDAO 
  13. 接口實現(xiàn)了 
  14. IntroductionAdvisor end... 

分享名稱:Spring引介增強IntroductionAdvisor使用
鏈接URL:http://www.5511xx.com/article/dpogdio.html