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

歡迎訪問 生活随笔!

生活随笔

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

javascript

学习 Spring (十三) AOP 配置

發布時間:2025/6/17 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习 Spring (十三) AOP 配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring入門篇 學習筆記

Spring 所有的切面和通知器都必須放在一個 內(可以配置包含多個 元素),每一個 可以包含 pointcut, advisor 和 aspect 元素(它們必須按照這個順序進行聲明)

風格的配置大量使用了 Spring 的自動代理機制

配置 Aspect

新建切面類:

public class MoocAspect {}

添加配置:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><bean id="moocAspect" class="com.karonda.aop.schema.advice.MoocAspect"></bean><aop:config><aop:aspect id="moocAspectAOP" ref="moocAspect"></aop:aspect></aop:config></beans>

配置 Pointcut

pointcut 類型說明詳見:pointcut expressions

新建類:

public class AspectBiz {}

修改配置文件:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><bean id="moocAspect" class="com.karonda.aop.schema.advice.MoocAspect"></bean><bean id="aspectBiz" class="com.karonda.aop.schema.advice.biz.AspectBiz"></bean><aop:config><aop:aspect id="moocAspectAOP" ref="moocAspect"><aop:pointcut id="moocPointcut" expression="execution(* com.karonda.aop.schema.advice.biz.*Biz.*(..))"/></aop:aspect></aop:config></beans>

Advice

添加依賴包 aspectjweaver:

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency>

修改 MoocAspect:

public class MoocAspect {public void before(){System.out.println("MoocAspect before.");}public void afterReturning(){System.out.println("MoocAspect afterReturning.");}public void afterThrowing(){System.out.println("MoocAspect afterThrowing.");}public void after(){System.out.println("MoocAspect after.");}// 環繞通知方法的第一個參數必須是 ProceedingJoinPoint 類型public Object around(ProceedingJoinPoint pjp){Object obj = null;try{System.out.println("MoocAspect around 1.");obj = pjp.proceed();System.out.println("MoocAspect around 2.");}catch (Throwable e){e.printStackTrace();}return obj;}public Object aroundInit(ProceedingJoinPoint pjp, String bizName, int times){System.out.println(bizName + " " + times);Object obj = null;try{System.out.println("MoocAspect aroundInit 1.");obj = pjp.proceed();System.out.println("MoocAspect aroundInit 2.");}catch (Throwable e){e.printStackTrace();}return obj;} }

修改 AspectBiz:

public class AspectBiz {public void biz() {System.out.println("AspectBiz biz."); // throw new RuntimeException();}public void init(String bizName, int times){System.out.println("AspectBiz init: " + bizName + " " + times);}}

修改配置:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><bean id="moocAspect" class="com.karonda.aop.schema.advice.MoocAspect"></bean><bean id="aspectBiz" class="com.karonda.aop.schema.advice.biz.AspectBiz"></bean><aop:config><aop:aspect id="moocAspectAOP" ref="moocAspect"><aop:pointcut expression="execution(* com.karonda.aop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/><!--前置通知--><aop:before method="before" pointcut-ref="moocPiontcut"/><!--返回后通知--><aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/><!--拋出異常通知--><aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut" /><!--后通知--><aop:after method="after" pointcut-ref="moocPiontcut"/><!--環繞通知--><aop:around method="around" pointcut-ref="moocPiontcut"/><!--帶參數--><aop:around method="aroundInit" pointcut="execution(* com.karonda.aop.schema.advice.biz.AspectBiz.init(String, int))and args(bizName, times)"/></aop:aspect></aop:config></beans>

添加測試類:

@RunWith(BlockJUnit4ClassRunner.class) public class TestAOPSchemaAdvice extends UnitTestBase {public TestAOPSchemaAdvice(){super("classpath:spring-aop-schema-advice.xml");}@Testpublic void testBiz(){AspectBiz biz = super.getBean("aspectBiz");biz.biz();}@Testpublic void testInit(){AspectBiz biz = super.getBean("aspectBiz");biz.init("moocService", 3);} }

源碼:learning-spring

轉載于:https://www.cnblogs.com/victorbu/p/10499476.html

總結

以上是生活随笔為你收集整理的学习 Spring (十三) AOP 配置的全部內容,希望文章能夠幫你解決所遇到的問題。

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