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

歡迎訪問 生活随笔!

生活随笔

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

javascript

第四章:Spring AOP

發布時間:2024/4/17 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第四章:Spring AOP 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

4.1:面向切面編程
AOP是在運行期間將代碼切入到類的指定位置的編程思想。切面能幫助我們模塊化橫切關注點,實現橫切關注點的復用。Spring在運行期間將切面植入到指定的Bean中,實際是通過攔截方法調用的過程中插入了切面。
4.2:描述切點
SpringAOP中切點的定義使用了AspectJ的切點表達式。但是只支持AspectJ的部分切點表達式。arg(),@arg(),this(),target(),@target(),within()$within(),@annotation和execution()。這些切點指示器中execution()是用來實現執行匹配的,其他指示器用來限定匹配的切點。
切點案例:
execution(* 包.包.類.方法(..))
execution(* 包.包.類.方法(..) && within (包A.*)) //附加條件是:包A下的任何方法被調用
execution(* 包.包.類.方法(..) && bean("beanID")) //附加條件是:匹配特定的bean
execution(* 包.包.類.方法(..) && !bean("beanID")) //附加條件是:不匹配特定的bean
execution(* 包.包.類.方法(int) && args(property)) //有參數的切點
4.3:使用注解創建切面
使用注解創建切面需要啟用AspectJ的自動代理,然后使用@After、@AfterReturning、@AfterThrowing、@Around、@Before注解配合java類創建切面。
啟動AspectJ自動代理:
在java配置類中啟動:
@Configuration
@EnableAspectJAutoProxy //啟動AspectJ自動代理
@ComponentScan
public class ConcertConfig(){@Bean...}
在xml中啟動:
<beans>
<aop:aspectJ-autoproxy> //啟動AspectJ自動代理
</beans>
使用注解創建切面
package xxx;
/**
切點=切面+通知
*/
@Aspect
public class Audience{
//切點
@PointCut("execution(* 包.包.類.方法(..))")
public void performance(){} //用于承載切點,方法體不重要,之后使用方法名可調用切面。
//通知
@Before("performance()")
public void helloBefore(){System.out.println("前置通知執行")}
@AfterReturning("performance()")
public void helloAfterReturning(){System.out.println("返回后置通知執行")
@AfterThrowing("performance()")
public void helloAfterThrowing(){System.out.println("異常后置通知執行")}
}
使用注解創建環繞通知
package xxx;
@Aspect
public class Audience{
//切點
@PointCut("execution(* 包.包.類.方法(..))")
public void performance(){} //用于承載切點,方法體不重要,之后使用方法名可調用切面。
//環繞通知
@Around("performance()")
public void helloAround(ProceedingJoinPoint jp){
try{
//執行前置通知
jp.proceed();
//執行后置通知
}catch(Throwable e){
//調用異常通知
}
}
}
4.4:在xml中聲明切面
一般切面
<aop:config>
<aop:aspect ref = "audience"> //ref 引用了ID為audience的Bean,這個bean中定義了通知方法。
<!--切點-->
<aop:pointcut id = "performance" expression = "execution(* 包.包.類.方法(..))" />
<!--環繞通知-->
<aop:before pointcut-ref = "performance" method = "通知A" />
<aop:after-returning pointcut-ref = "performance" method = "通知B" />
<aop:after-throwing> pointcut-ref = "performance" method = "通知C'/>
</aop:aspect>
</aop:config>
環繞通知切面
<aop:config>
<aop:aspect ref = "audience">
<!--切點-->
<aop:pointcut id = "performance" expression = "execution(* 包.包.類.方法(..))" />
<!--環繞通知-->
<aop:around pointcut-ref = "performance" method = "helloAround"> //指點環繞通知方法
</aop:aspect>
</aop:config>
4.5:注入AspectJ切面
???????????????????

















轉載于:https://www.cnblogs.com/Xmingzi/p/9007553.html

總結

以上是生活随笔為你收集整理的第四章:Spring AOP的全部內容,希望文章能夠幫你解決所遇到的問題。

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