日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

注解的方式实现动态代理基于SpringAOP

發布時間:2025/4/14 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 注解的方式实现动态代理基于SpringAOP 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.配置spring容器

導入jar包  

    com.springsource.net.sf.cglib-2.2.0.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    commons-logging-1.1.3.jar
    spring-aop-4.2.2.RELEASE.jar
    spring-beans-4.2.2.RELEASE.jar
    spring-context-4.2.2.RELEASE.jar
    spring-core-4.2.2.RELEASE.jar
    spring-expression-4.2.2.RELEASE.jar

配置xml文件

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd"> 13 14 <!-- 包掃描 --> 15 <context:component-scan base-package="com.eduask.liusheng.aop.after"/> 16 17 <!-- 開啟切面 --> 18 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 19 </beans> applicationContext-aop-annotation.xml

2.ArithmeticCaluda.java

1 public interface ArithmeticCaluda { 2 3 /** 4 * 加 5 * @param a 6 * @param b 7 * @return 8 */ 9 double add(double a, double b); 10 11 /** 12 * 減 13 * @param a 14 * @param b 15 * @return16 */ 17 double sub(double a, double b); 18 19 /** 20 * 乘 21 * @param a 22 * @param b 23 * @return24 */ 25 double mul(double a, double b); 26 27 /** 28 * 除 29 * @param a 30 * @param b 31 * @return32 */ 33 double div(double a, double b); 34 35 } ArithmeticCaluda.java

3.ArithmeticCaludaImp.java

import org.springframework.stereotype.Component;@Component public class ArithmeticCaludaImp implements ArithmeticCaluda {/*** 加* @param a* @param b* @return*/public double add(double a,double b){double result=a+b;return result;}/*** 減* @param a* @param b* @return*/public double sub(double a,double b){double result=a-b;return result;}/*** 乘* @param a* @param b* @return*/public double mul(double a,double b){double result=a*b;return result;}/*** 除* @param a* @param b* @return*/public double div(double a,double b){double result=a/b; // int i=10/0;return result;} } ArithmeticCaludaImp.java

4.Log.java

1 import java.util.Arrays; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.After; 5 import org.aspectj.lang.annotation.AfterReturning; 6 import org.aspectj.lang.annotation.AfterThrowing; 7 import org.aspectj.lang.annotation.Aspect; 8 import org.aspectj.lang.annotation.Before; 9 import org.springframework.core.annotation.Order; 10 import org.springframework.stereotype.Component; 11 12 @Component 13 @Aspect 14 @Order(0) 15 public class Log { 16 17 @Before("execution(* com.eduask.liusheng.aop.after.*.*(..))") 18 public void before(JoinPoint jp){ 19 System.out.println("the method "+jp.getSignature().getName()+"() begin with "+Arrays.asList(jp.getArgs())); 20 } 21 22 @After("execution(* com.eduask.liusheng.aop.after.*.*(..))") 23 public void after(JoinPoint jp){ 24 System.out.println("the method after "); 25 } 26 27 @AfterReturning(value="execution(* com.eduask.liusheng.aop.after.*.*(..))",returning="result") 28 public void afterReturning(JoinPoint jp,Object result){ 29 System.out.println("the method "+jp.getSignature().getName()+"() end with ["+result+"]"); 30 } 31 32 @AfterThrowing(value="execution(* com.eduask.liusheng.aop.after.*.*(..))",throwing="e") 33 public void afterThrowing(Exception e){ 34 System.out.println("afterThrowing"+e.getMessage()); 35 } 36 } Log.java

5.CopyOfLog.java

1 import java.util.Arrays; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.After; 5 import org.aspectj.lang.annotation.AfterReturning; 6 import org.aspectj.lang.annotation.AfterThrowing; 7 import org.aspectj.lang.annotation.Aspect; 8 import org.aspectj.lang.annotation.Before; 9 import org.aspectj.lang.annotation.Pointcut; 10 import org.springframework.core.annotation.Order; 11 import org.springframework.stereotype.Component; 12 13 @Component 14 @Aspect 15 @Order(1) 16 public class CopyOfLog { 17 @Pointcut("execution(* com.eduask.liusheng.aop.after.*.*(..))") 18 public void log(){} 19 20 @Before("log()") 21 public void before(JoinPoint jp){ 22 System.out.println("CopyOfLog the method "+jp.getSignature().getName()+"() begin with "+Arrays.asList(jp.getArgs())); 23 } 24 25 @After("log()") 26 public void after(JoinPoint jp){ 27 System.out.println("CopyOfLog the method "+jp.getSignature().getName()+"() end "); 28 } 29 30 @AfterReturning(value="log()",returning="re") 31 public void afterReturning(JoinPoint jp,Object re){ 32 System.out.println("CopyOfLog the method "+jp.getSignature().getName()+"() end with ["+re+"]"); 33 } 34 35 @AfterThrowing(value="execution(* com.eduask.liusheng.aop.after.*.*(..))",throwing="e") 36 public void afterThrowing(Exception e){ 37 System.out.println("afterThrowing"+e.getMessage()); 38 } 39 } CopyOfLog.java

6.Test.java

1 import org.springframework.context.ApplicationContext; 2 import org.springframework.context.support.ClassPathXmlApplicationContext; 3 4 import com.eduask.liusheng.aop.after.ArithmeticCaluda; 5 /** 6 * aop之注解實現動態代理,使用Aspect之分步控制連接點 7 * @author Administrator 8 * 9 */ 10 public class Test { 11 public static void main(String[] args) { 12 ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext-aop-annotation.xml"); 13 ArithmeticCaluda arithmeticCaluda=(ArithmeticCaluda) app.getBean("arithmeticCaludaImp"); 14 arithmeticCaluda.div(10, 2); 15 } 16 } Test,java

注解說明:

@Component :spring組件,spring初始化的時候,spring會把所有添加@Component注解的類作為使用自動掃描注入配置路徑下的備選對象,同時在初始化spring@Autowired

@Aspect :聲明一個切面
@Order(1) :當有多個切面時,配置優先級,數值越小優先級越高,

@Pointcut("execution(* com.eduask.liusheng.aop.after.*.*(..))") : @Pointcut聲明連接點,execution中第一個*表示所有返回類型,第二個*表示所有類,第三個*表示所有方法,(..)表示所有參數類型

@Before("log()") :連接點方法之前執行該方法

@After("log()") ?:連接點方法之后執行該方法,方法return之前

@AfterReturning(value="log()",returning="re") ?:連接點方法之后執行該方法,方法return之后,re用來接收返回值

@AfterThrowing(value="execution(* com.eduask.liusheng.aop.after.*.*(..))",throwing="e") :連接點方法出現異常時執行

測試結果:

正常運行時

the method div() begin with [10.0, 2.0]
CopyOfLog the method div() begin with [10.0, 2.0]
CopyOfLog the method div() end
CopyOfLog the method div() end with [5.0]
the method after
the method div() end with [5.0]

異常運行時

the method div() begin with [10.0, 2.0]
CopyOfLog the method div() begin with [10.0, 2.0]
CopyOfLog the method div() end
afterThrowing/ by zero
the method after
afterThrowing/ by zero

?

7.@Around環繞通知,功能整合一步到位

AroundLog.java

1 import java.util.Arrays; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 import org.aspectj.lang.annotation.Around; 5 import org.aspectj.lang.annotation.Aspect; 6 import org.springframework.stereotype.Component; 7 8 @Component 9 @Aspect 10 public class AroundLog { 11 12 @Around("execution(* com.eduask.liusheng.aop.around.*.*(..))") 13 public Object logArround(ProceedingJoinPoint jp) throws Throwable{ 14 Object result=null; 15 System.out.println("Arround the method "+jp.getSignature().getName()+"() begin with "+Arrays.asList(jp.getArgs())); 16 try { 17 result=jp.proceed(); 18 System.out.println("Arround the method "+jp.getSignature().getName()+"() end "); 19 } catch (Throwable e) { 20 System.out.println("Arround the method "+e.getMessage()); 21 throw e; 22 } 23 System.out.println("Arround the method "+jp.getSignature().getName()+"() end with ["+result+"]"); 24 return result; 25 } 26 } AroundLog.java

?

轉載于:https://www.cnblogs.com/qq634571685/p/7168845.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的注解的方式实现动态代理基于SpringAOP的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。