成人免费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-03-26 10:14:49

物聯網增強現實IOT

2021-10-02 10:24:35

Android端Firefox 93密碼

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腳本語言操作系統

2015-02-13 09:44:02

2023-07-30 15:00:21

2025-01-20 07:00:00

2009-12-28 09:51:17

Fedora GNOM

2021-05-24 15:36:44

人工智能機器學習技術

2009-10-09 13:42:56

Spring DataSpring DM

2015-02-09 09:57:56

Ceph 塊設備OpenStackLinux

2010-04-08 16:49:26

Visual StudMVC 2.0

2023-05-10 08:17:22

合并事件推送
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 777zyz色资源站在线观看 | 亚洲综合视频 | 日韩精品久久久久 | 91大神在线资源观看无广告 | 一区二区免费在线观看 | 欧美中文字幕一区二区三区亚洲 | 在线播放91 | 亚洲免费一区二区 | 一区| 午夜tv免费观看 | 日本视频在线 | 中国一级特黄真人毛片免费观看 | 久久夜视频 | a级毛片毛片免费观看久潮喷 | 欧美黄色一区 | 久久久性色精品国产免费观看 | 精品视频一区二区 | 久久久精彩视频 | 青青久草 | 视频一区欧美 | www国产亚洲精品久久网站 | h小视频 | 国产一区免费 | 337p日本欧洲亚洲大胆精蜜臀 | 91免费福利视频 | 欧美激情国产日韩精品一区18 | 久久伊人亚洲 | 正在播放国产精品 | 国产一区二区三区在线 | 在线高清免费观看视频 | 欧美日韩三级 | 久久久久久久av | 成人在线播放 | 在线免费亚洲视频 | 成人一区二区在线 | 欧美国产精品 | 激情网站 | 97精品久久 | 成人免费在线播放视频 | 日韩欧美在线观看 | 亚洲一区视频在线 |