日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

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

生活随笔

當(dāng)前位置: 首頁(yè) >

AOP:【动态代理】||@Pointcut

發(fā)布時(shí)間:2025/4/16 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AOP:【动态代理】||@Pointcut 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

AOP:【動(dòng)態(tài)代理】 * 指在程序運(yùn)行期間動(dòng)態(tài)的將某段代碼切入到指定方法指定位置進(jìn)行運(yùn)行的編程方式

* 1、導(dǎo)入aop模塊;Spring AOP:(spring-aspects)

<dependency>
?? <groupId>org.springframework</groupId>
?? <artifactId>spring-aspects</artifactId>
?? <version>4.3.12.RELEASE</version>
</dependency>

* 2、定義一個(gè)業(yè)務(wù)邏輯類(MathCalculator);在業(yè)務(wù)邏輯運(yùn)行的時(shí)候?qū)⑷罩具M(jìn)行打印(方法之前、方法運(yùn)行結(jié)束、方法出現(xiàn)異常,xxx)
* 3、定義一個(gè)日志切面類(LogAspects):切面類里面的方法需要?jiǎng)討B(tài)感知MathCalculator.div運(yùn)行到哪里然后執(zhí)行;
*???? 通知方法
*??????? 前置通知(@Before):logStart:在目標(biāo)方法(div)運(yùn)行之前運(yùn)行
*??????? 后置通知(@After):logEnd:在目標(biāo)方法(div)運(yùn)行結(jié)束之后運(yùn)行(無(wú)論方法正常結(jié)束還是異常結(jié)束)
*??????? 返回通知(@AfterReturning):logReturn:在目標(biāo)方法(div)正常返回之后運(yùn)行
*??????? 異常通知(@AfterThrowing):logException:在目標(biāo)方法(div)出現(xiàn)異常以后運(yùn)行
*??????? 環(huán)繞通知(@Around):動(dòng)態(tài)代理,手動(dòng)推進(jìn)目標(biāo)方法運(yùn)行(joinPoint.proceed()
* 4、給切面類的目標(biāo)方法標(biāo)注何時(shí)何地運(yùn)行(通知注解)
* 5、將切面類和業(yè)務(wù)邏輯類(目標(biāo)方法所在類)都加入到容器中;
* 6、必須告訴Spring哪個(gè)類是切面類(給切面類上加一個(gè)注解:@Aspect?)
* 7、給配置類中加 @EnableAspectJAutoProxy 【開啟基于注解的aop模式】
*???? 在Spring中很多的 @EnableXXX;

* 三步: * 1)、將業(yè)務(wù)邏輯組件和切面類都加入到容器中;告訴Spring哪個(gè)是切面類(@Aspect) * 2)、在切面類上的每一個(gè)通知方法上標(biāo)注通知注解,告訴Spring何時(shí)何地運(yùn)行(切入點(diǎn)表達(dá)式) * 3)、開啟基于注解的aop模式;@EnableAspectJAutoProxy

MathCalculator.java

package com.dym.aop;public class MathCalculator {public int div(int i,int j){System.out.println("MathCalculator...div...");return i/j; }}

LogAspects.java

package com.dym.aop;import java.util.Arrays;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut;/*** @Aspect: 告訴Spring當(dāng)前類是一個(gè)切面類*/ @Aspect public class LogAspects {//抽取公共的切入點(diǎn)表達(dá)式//1、本類引用//2、其他的切面引用@Pointcut("execution(public int com.dym.aop.MathCalculator.*(..))")public void pointCut() {};//@Before在目標(biāo)方法之前切入;切入點(diǎn)表達(dá)式(指定在哪個(gè)方法切入)@Before("pointCut()")public void logStart(JoinPoint joinPoint) {Object[] args = joinPoint.getArgs();System.out.println("" + joinPoint.getSignature().getName() + "運(yùn)行。。。@Before:參數(shù)列表是:{" + Arrays.asList(args) + "}");}@After("com.dym.aop.LogAspects.pointCut()")public void logEnd(JoinPoint joinPoint) {System.out.println("" + joinPoint.getSignature().getName() + "結(jié)束。。。@After");}//JoinPoint一定要出現(xiàn)在參數(shù)表的第一位@AfterReturning(value = "pointCut()", returning = "result")public void logReturn(JoinPoint joinPoint, Object result) {System.out.println("" + joinPoint.getSignature().getName() + "正常返回。。。@AfterReturning:運(yùn)行結(jié)果:{" + result + "}");}@AfterThrowing(value = "pointCut()", throwing = "exception")public void logException(JoinPoint joinPoint, Exception exception) {System.out.println("" + joinPoint.getSignature().getName() + "異常。。。異常信息:{" + exception + "}");}}

MainConfigOfAOP.java

package com.dym.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy;import com.dym.aop.LogAspects; import com.dym.aop.MathCalculator;@EnableAspectJAutoProxy @Configuration public class MainConfigOfAOP {//業(yè)務(wù)邏輯類加入容器中@Beanpublic MathCalculator calculator(){return new MathCalculator();}//切面類加入到容器中@Beanpublic LogAspects logAspects(){return new LogAspects();} }

IOCTest_AOP.java

package com.dym.test;import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.dym.aop.MathCalculator; import com.dym.config.MainConfigOfAOP;public class IOCTest_AOP {@Testpublic void test01(){AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);//1、不要自己創(chuàng)建對(duì)象 // MathCalculator mathCalculator = new MathCalculator(); // mathCalculator.div(1, 1);MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);mathCalculator.div(1, 1);applicationContext.close();}}

???????

總結(jié)

以上是生活随笔為你收集整理的AOP:【动态代理】||@Pointcut的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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