自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring 引介增強(qiáng)IntroductionAdvisor使用

開發(fā) 前端
在不修改業(yè)務(wù)代碼的情況下如何讓某個(gè)類具有某項(xiàng)功能呢,比如具有XXXDAO接口的能力?IntroductionAdvisor只能應(yīng)用于Introduction類型的通知,而PointcutAdvisor可以應(yīng)用于所有類型的通知

[[423979]]

環(huán)境:Spring5.2.14

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

1 IntroductionAdvisor介紹

IntroductionAdvisor與PointcutAdvisor區(qū)別

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

2 IntroductionAdvisor使用流程

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

2.1 目標(biāo)接口

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

2.2 Introduction攔截器

  1. public class CustomIntroductionInterceptor implements IntroductionInterceptor, DesignDAO { 
  2.     // 判斷當(dāng)前的攔截器是否實(shí)現(xiàn)了目標(biāo)接口(DesignDAO,我們需要某個(gè)類具有指定接口的功能) 
  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í)行的方法所屬類是否實(shí)現(xiàn)了目標(biāo)接口 
  12.         // 這里必須要進(jìn)行相應(yīng)的判斷攔截,否則會(huì)在沒有被攔截的類方法執(zhí)行的時(shí)候報(bào)錯(cuò)我 
  13.         // 因?yàn)槟愕钠渌愃鶎?duì)應(yīng)的接口并沒有在該攔截器中被實(shí)現(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("接口實(shí)現(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.     // 定義了所要實(shí)現(xiàn)的所有接口 
  17.     @Override 
  18.     public Class<?>[] getInterfaces() { 
  19.         return new Class<?>[] {DesignDAO.class} ; 
  20.     } 
  21.  
  22.     // 過濾類,返回true就會(huì)被匹配 
  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.     // 在這里我們可以校驗(yàn)我們的Advice類是否實(shí)現(xiàn)了執(zhí)行的接口getInterfaces中定義的接口 
  34.     @Override 
  35.     public void validateInterfaces() throws IllegalArgumentException { 
  36.         // 這里可以參考DefaultIntroductionAdvisor的實(shí)現(xiàn) 
  37.     } 
  38.  

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

2.4 驗(yàn)證

  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ù)類并沒有實(shí)現(xiàn)DesignDAO接口。接下來看看通過上面定義IntroductionAdvisor是否可以讓我們的業(yè)務(wù)類具有目標(biāo)類的功能

  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. 接口實(shí)現(xiàn)了 
  14. IntroductionAdvisor end... 

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2024-07-05 11:22:39

2022-12-23 10:37:41

JavaScript文檔

2023-12-01 10:51:00

LoRaWAN醫(yī)療保健

2021-03-26 10:14:49

物聯(lián)網(wǎng)增強(qiáng)現(xiàn)實(shí)IOT

2021-10-02 10:24:35

Android端Firefox 93密碼

2010-01-08 12:11:04

ibmdwWeb

2024-11-27 08:00:00

代碼圖代碼分析開發(fā)

2024-07-03 09:38:35

LLM人工智能

2022-03-03 10:00:28

CiliumKubernetes開源

2020-03-01 17:49:16

Linux腳本語言操作系統(tǒng)

2023-04-07 14:04:52

增強(qiáng)分析人工智能

2009-12-28 09:51:17

Fedora GNOM

2023-07-30 15:00:21

2015-02-13 09:44:02

2025-01-20 07:00:00

2009-10-09 13:42:56

Spring DataSpring DM

2023-03-10 09:41:16

NAPI框架鴻蒙

2009-11-10 11:40:33

VB.NET運(yùn)算操作

2024-03-15 11:02:19

數(shù)據(jù)中心B2BCXAR驅(qū)動(dòng)

2023-05-10 08:17:22

合并事件推送
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)