成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring 引介增強IntroductionAdvisor使用

開發 前端
在不修改業務代碼的情況下如何讓某個類具有某項功能呢,比如具有XXXDAO接口的能力?IntroductionAdvisor只能應用于Introduction類型的通知,而PointcutAdvisor可以應用于所有類型的通知

[[423979]]

環境:Spring5.2.14

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

1 IntroductionAdvisor介紹

IntroductionAdvisor與PointcutAdvisor區別

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

2 IntroductionAdvisor使用流程

假定我們的業務類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.     // 判斷當前的攔截器是否實現了目標接口(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.         // 判斷當前執行的方法所屬類是否實現了目標接口 
  12.         // 這里必須要進行相應的判斷攔截,否則會在沒有被攔截的類方法執行的時候報錯我 
  13.         // 因為你的其它類所對應的接口并沒有在該攔截器中被實現。 
  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("接口實現了") ; 
  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.     // 定義了所要實現的所有接口 
  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類是否實現了執行的接口getInterfaces中定義的接口 
  34.     @Override 
  35.     public void validateInterfaces() throws IllegalArgumentException { 
  36.         // 這里可以參考DefaultIntroductionAdvisor的實現 
  37.     } 
  38.  

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

2.4 驗證

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

 業務類并沒有實現DesignDAO接口。接下來看看通過上面定義IntroductionAdvisor是否可以讓我們的業務類具有目標類的功能

  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.      

 執行結果:

  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. 我被調用了... 
  6. 保存方法...class com.pack.dao.CustomerDAOImpl 
  7. 我是通知類:IntroductionInterceptor, interface com.pack.dao.CustomDAO 
  8. intf: interface com.pack.dao.CustomDAO 
  9. 更新數據...class com.pack.dao.CustomerDAOImpl 
  10. IntroductionAdvisor start... 
  11. 我是通知類:IntroductionInterceptor, interface com.pack.interfaces.DesignDAO 
  12. intf: interface com.pack.interfaces.DesignDAO 
  13. 接口實現了 
  14. IntroductionAdvisor end... 

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2024-07-05 11:22:39

2022-12-23 10:37:41

JavaScript文檔

2023-12-01 10:51:00

LoRaWAN醫療保健

2021-10-02 10:24:35

Android端Firefox 93密碼

2021-03-26 10:14:49

物聯網增強現實IOT

2010-01-08 12:11:04

ibmdwWeb

2024-11-27 08:00:00

代碼圖代碼分析開發

2024-07-03 09:38:35

LLM人工智能

2022-03-03 10:00:28

CiliumKubernetes開源

2023-04-07 14:04:52

增強分析人工智能

2020-03-01 17:49:16

Linux腳本語言操作系統

2009-12-28 09:51:17

Fedora GNOM

2015-02-13 09:44:02

2023-07-30 15:00:21

2025-01-20 07:00:00

2021-05-24 15:36:44

人工智能機器學習技術

2009-10-09 13:42:56

Spring DataSpring DM

2023-03-10 09:41:16

NAPI框架鴻蒙

2009-11-10 11:40:33

VB.NET運算操作

2012-09-25 09:43:45

Windows 8Ubuntu
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产一区二区高清在线 | 日韩一及片 | 日本不卡视频 | 国产精品高潮呻吟久久av野狼 | 99久久99久久精品国产片果冰 | 欧美色综合| 日韩中文字幕在线视频 | 欧美日韩国产三级 | 九色网址| 成人精品一区二区三区 | 国产成人免费视频网站视频社区 | 国产三级精品三级在线观看四季网 | 欧美日韩久久 | 2023亚洲天堂 | 美女日批免费视频 | 综合久久亚洲 | 日本三级线观看 视频 | 亚洲欧美综合精品久久成人 | 成人免费在线 | 日韩欧美在线免费观看 | 中文字幕在线播放第一页 | 草久久| 国产成人免费视频网站视频社区 | 精品啪啪 | 欧美国产一区二区 | 久久精品视频亚洲 | 欧美精品一区二区三区在线 | 丁香婷婷久久久综合精品国产 | 一区二区三区免费在线观看 | 韩国精品在线 | 国产成人免费视频网站视频社区 | 国产三级一区二区三区 | 国产丝袜人妖cd露出 | 久久亚洲综合 | 91在线免费观看网站 | 午夜视频在线免费观看 | 人人九九精 | 国产激情一区二区三区 | 国产精品精品久久久 | 水蜜桃亚洲一二三四在线 | 日韩精品视频一区二区三区 |