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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring对AspectJ的支持

發(fā)布時(shí)間:2023/12/10 javascript 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring对AspectJ的支持 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.AspectJ介紹及Pointcut注解應(yīng)用

(1)AspectJ

  • @AspectJ的風(fēng)格類(lèi)似純java注解的普通java類(lèi)
  • Spring可以使用AspectJ來(lái)做切入點(diǎn)解析
  • AOP的運(yùn)行時(shí)仍舊是純的Spring AOP,對(duì)AspectJ的編譯器或者織入無(wú)依賴性

(2)Spring中配置@AspectJ

  • 對(duì)@AspectJ支持可以使用XML或者Java風(fēng)格的配置
  • 確保AspectJ的aspectjweaver.jar庫(kù)包含在應(yīng)用程序(版本1.6.8或者更高版本)的classpath中

java注解方式:

@Configuration @EnableAspectJAutoProxy public class AppConfig{}

xml方式:

<aop:aspectj-autoproxy/>

(3)@Aspect注解的使用

  • @AspectJ切面使用@Aspect注解配置,任何擁有@Aspect的bean將被Spring自動(dòng)識(shí)別并應(yīng)用
  • 用@Aspect注解的類(lèi)可以有方法和字段,他們也可能包括切入點(diǎn)(pointcut),通知(advice)和引入(introduction)聲明
  • @Aspect注解是不能夠通過(guò)類(lèi)路徑自動(dòng)檢測(cè)發(fā)現(xiàn)的,所以需要配合使用@Component注釋或者在xml配置bean

   1)在xml中配置bean

<bean id="myAspect" class="org.xyz.NotVeryUsefulAspect"><!--configure properties of aspect here as nomal--> </bean>

   2)使用@Component注釋

package org.xyz; import org.sapectj.lang.annotation.Aspect;@Aspect public class NotVeryUsefulAspect{ }
  • 一個(gè)類(lèi)中的@Aspect注解標(biāo)識(shí)它為一個(gè)切面,并且將自己從自動(dòng)代理中排除,否則會(huì)出現(xiàn)死循環(huán)

(4)pointcut

  • 一個(gè)切入點(diǎn)通過(guò)一個(gè)普通的方法定義來(lái)提供,并且切入點(diǎn)表達(dá)式使用@Pointcut注解,方法返回類(lèi)型必須為void
  • 定義一個(gè)名為‘a(chǎn)nyOldTransfer’,這個(gè)切入點(diǎn)將匹配任何名為“transfer”的方法的執(zhí)行
@Pointcut("execution(* transfer(..))")//the pointcut expression private void anyOldTransfer(){}//the pointcut signature
  • 切入點(diǎn)支持的定義方式
    • execution:匹配方法執(zhí)行的連接點(diǎn)
    • within:限定匹配特定類(lèi)型的連接點(diǎn)
    • this:限定匹配特定連接點(diǎn)的bean引用是指定類(lèi)型的實(shí)例
    • target:限定匹配特定連接點(diǎn)的目標(biāo)對(duì)象是指定類(lèi)型的實(shí)例
    • args:限定匹配特定連接點(diǎn)的參數(shù)是指定類(lèi)型的實(shí)例
    • @target:限定匹配特定連接點(diǎn)的類(lèi)執(zhí)行對(duì)象的具有給定類(lèi)型的注解
    • @args:限定匹配特定連接點(diǎn)實(shí)際傳入?yún)?shù)的類(lèi)型具有給定類(lèi)型的注解‘
    • @within:限定匹配到具有給定的注釋類(lèi)型的連接點(diǎn)
    • @annotation:限定匹配特定連接點(diǎn)的主體具有給定的注解

(5)組合pointcut

  • 切入點(diǎn)表達(dá)式可以通過(guò)&&、||、!進(jìn)行組合,也可以通過(guò)名字引入切入點(diǎn)表達(dá)式
  • 通過(guò)組合,可以建立更加復(fù)雜的切入點(diǎn)表達(dá)式
@Pointcut("execution(public *(..))") private void anyPublicOperation(){}@Pointcut("within(com.xyz.someapp.trading..)") private void inTrading(){}@Pointcut("anyPublicOperation() && inTrading()") private void tradingOperation(){}

(6)定義良好的pointcuts

  • AspectJ是編譯期的AOP
  • 檢查代碼并匹配連接點(diǎn)與切入點(diǎn)的代價(jià)是昂貴的
  • 一個(gè)好的切入點(diǎn)應(yīng)該包括以下幾點(diǎn)
    • 選擇特定類(lèi)型的連接點(diǎn),如:execution,get,set,call,handler
    • 確定連接點(diǎn)范圍,如:within,withincode
    • 匹配上下文信息,如:this,target,@annotation

2.Advice定義及實(shí)例

(1)Before advice

import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;@Component @Aspect public class ExampleAspect{//在執(zhí)行com.xyz.aop.aspectj包下以Biz結(jié)尾的類(lèi)的所有方法時(shí)匹配Advice@Before("*execution(* com.xyz.aop.aspectj.biz.*Biz.*(..))")public void before(){//.. } }

(2)After returning advice

@Aspect public class AfterReturningExample{@AfterReturning("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")public void doAccessCheck(){//... } }
  • 有時(shí)候需要在通知體內(nèi)得到返回的實(shí)際值,可以使用@AfterReturning綁定返回值的形式
@Aspect public class AfterReturningExample{@AfterReturning(pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",returning="retVal")public void doAccessCheck(Object retVal){//... } }

(3)After throwing advice

@Aspect public class AfterThrowingExample{@AfterThrowing("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")public void doRecoveryActions(){//... } }
  • 有時(shí)候需要在通知體內(nèi)得到返回的實(shí)際值,可以使用@AfterReturning綁定的返回值的形式
@Aspect public class AfterThrowingExample{@AfterThrowing(pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",throwing="ex")public void doRecoveryActions(DataAccessException ex){//... } }

(4)After (finally) advice

  • 最終通知必須準(zhǔn)備處理正常和異常兩種返回情況,它通常用于釋放資源
@Aspect public class AfterFinallyExample{@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")public void doReleaseLock(){//... } }

(5)Around advice

  • 環(huán)繞通知使用@Around注解來(lái)聲明,通知方法的第一個(gè)參數(shù)必須是ProceedingJoinPoint類(lèi)型
  • 在通知內(nèi)部調(diào)用ProceedingJoinPoint的proceed()方法會(huì)導(dǎo)致執(zhí)行真正的方法,傳入一個(gè)Object[]對(duì)象,數(shù)組中的值將被作為參數(shù)傳遞給方法
@Aspect public class AroundExample{@Around("com.xyz.myapp.SystemArchitecture.businessService()")public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{//start stopwatchObject retVal=pjp.proceed();//stop stopwatchreturn retVal;} }

3.Advice擴(kuò)展

(1)給advice傳遞參數(shù)

方式一:

@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation() && args(account,..)") public void validateAccount(Account account){//... }

方式二:

@Pointcut("com.xyz.myapp.SystemArchitecture.dataAccessOperation() && args(account,..)") private void accountDataAccessOperation(Account account){}@Before("accountDataAccessOperation(account)") //Account account:這里的方法參數(shù)可以是任何類(lèi)的對(duì)象 public void validateAccount(Account account){//... }

方式三:

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Auditable{Auditable value(); } @Before("com.xyz.lib.Pointcuts.anyPublicMethod()&&@annotation(auditable)") public void audit(Auditable auditable){AuditCode code=auditable.value();//... }

(2)Advice的參數(shù)及泛型

  • Spring AOP可以處理泛型類(lèi)的聲明和使用方法的參數(shù)
public interface Sample<T>{void sampleGenericMethod(T param);void sampleGenericCollectionMethod(Collection>T>param); }

方式一:

@Before("execution(* ..Sample+.sampleGenericMethod(*)) && args(param)") public void beforeSampleMethod(MyType param){//Advice implementation }

方式二:

@Before("execution(* ..Sample+.sampleGenericCollectionMethod(*)) && args(param)") public void beforeSampleMethod(Collection<MyType> param){//Advice implementation }

(3)Advice參數(shù)名稱

  • 通知和切入點(diǎn)注解有一個(gè)額外的“argNames”,它可以用來(lái)指定所注解的方法的參數(shù)名
@Before(value="com.xyz.lib.Pointcuts.anyPublicMethod() && target(bean) && @annotation(auditable)",argNames="bean,auditable") public void audit(Object bean,Auditable auditable){AuditCode code=auditable.value();//...use code and bean }
  • 如果第一個(gè)參數(shù)是JoinPoint,ProceedingJoinPoint,JoinPoint.StaticPart,那么可以忽略它
@Before(value="com.xyz.lib.Pointcuts.anyPublicMethod() && target(bean) && @annotation(auditable)",argNames="bean,auditable") public void audit(JoinPoint jp,Object bean,Auditable auditable){AuditCode code=auditable.value();//...use code ,bean and jp }

(4)Introductions

  • 允許一個(gè)切面聲明一個(gè)通知對(duì)象實(shí)現(xiàn)指定接口,并且提供了一個(gè)接口實(shí)現(xiàn)類(lèi)來(lái)代表這些對(duì)象
  • introduction使用@DeclareParents進(jìn)行注解,這個(gè)注解用來(lái)定義匹配的類(lèi)型擁有一個(gè)新的parent
  • 例如:給定一個(gè)接口UsageTracked,并且該接口擁有DefaultUsageTracked的實(shí)現(xiàn),接下來(lái)的切面生命了所有的service接口的實(shí)現(xiàn)都實(shí)現(xiàn)了UsageTracked接口
@Aspect public class UsageTracking{@DeclareParents(value="com.xyz.myapp.service.*+",defaultImpl=DefaultUsageTracked.class)public static UsageTracked mixin;@Before("com.xyz.myapp.SystemArchitecture.businessService() && this(usageTracked)")public void recordUsage(UsageTracked usageTracked){usageTracked.incrementUseCount();} }

(5)切面實(shí)例化模型

  • 這是一個(gè)高級(jí)主題
  • “perthis”切面通過(guò)制定@Aspect注解perthis子句實(shí)現(xiàn)
  • 每個(gè)獨(dú)立的service對(duì)象執(zhí)行時(shí)都會(huì)創(chuàng)建一個(gè)切面實(shí)例
  • service對(duì)象的每個(gè)方法在第一次執(zhí)行的時(shí)候創(chuàng)建切面實(shí)例,切面在service對(duì)象失效的同時(shí)失效
@Aspect("perthis(com.xyz.myapp.SystemArchitecture.businessService())") public class MyAspect{private int someState;@Before(com.xyz.myapp.SystemArchitecture.businessService())public void recordServiceUsage(){//... } }

轉(zhuǎn)載于:https://www.cnblogs.com/chanaichao/p/9267173.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的Spring对AspectJ的支持的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。