©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一、第一步,引用依賴類,在Pom.xml加入依賴 <dependencies> <dependency> <groupId>org.springframework</groupI ...
================================
©Copyright 蕃薯耀 2020-01-21
https://www.cnblogs.com/fanshuyao/
一、第一步,引用依賴類,在Pom.xml加入依賴
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.12.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
二、第二步:增加配置類
1、@Configuration:聲明該類為配置類
2、@ComponentScan("com.lqy.spring.aop"):掃描相應的類,納入spring容器中管理
3、@EnableAspectJAutoProxy:啟用註解方式的Aop模式
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @ComponentScan("com.lqy.spring.aop") @EnableAspectJAutoProxy public class AopConfig { }
三、第三步:自定義邏輯運算
import org.springframework.stereotype.Component; /** * Calculator類需要在spring容器才能使用aop * 使用:@Component,同時使用@ComponentScan註解掃描時,要掃描到該類 * */ @Component public class Calculator { public int divInteger(int a, int b) { System.out.println("除法運算"); return a/b; } public double div(double a, double b) { System.out.println("除法運算"); return a/b; } public double add(double a, double b) { System.out.println("加法運算"); return a + b; } }
四、第四步:運算邏輯類切麵註入類
1、@Before:方法執行之前
2、@After:方法執行之後(不管會不會出現異常都會執行)
3、@AfterReturning:方法正常執行返回之後
4、@AfterThrowing:方法發生異常執行
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; import org.springframework.stereotype.Component; /** * 類需要在spring容器才能使用aop,並且添加切麵類的註解:@Aspect * */ @Aspect @Component public class CalculatorAop { /** * 公共切點 */ @Pointcut("execution( * com.lqy.spring.aop.Calculator.*(..))") public void pointCut() {} /** * 方法執行之前 */ @Before(value = "execution( * com.lqy.spring.aop.Calculator.*(..))") public void before(JoinPoint joinPoint) { System.out.println(""); System.out.println("==============================================================="); System.out.println("before方法:{" + joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName() + "}開始執行:"); System.out.println("方法參數是:{" + Arrays.asList(joinPoint.getArgs()) + "}"); } /** * 方法執行之後(不管會不會出現異常都會執行) * pointCut():使用公共的切點表達式 */ @After("pointCut()") public void after(JoinPoint joinPoint) { System.out.println("after方法:{" + joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName() + "}執行結束。"); } /** * 方法正常執行返回之後 */ @AfterReturning(value = "pointCut()", returning = "returnResult") public void afterReturn(JoinPoint joinPoint, Object returnResult) { System.out.println("afterReturn方法:{" + joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName() + "}執行返回的結果是:{" + returnResult + "}。"); System.out.println(""); } /** * 方法出現異常執行 */ @AfterThrowing(value = "pointCut()", throwing = "ex") public void afterThrowing(JoinPoint joinPoint, Exception ex) { System.out.println("afterThrowing方法:{" + joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName() + "}發生異常:" + ex); } }
五、第五步:測試
import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.lqy.spring.aop.Calculator; import com.lqy.spring.config.AopConfig; public class TestAop { private AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AopConfig.class); @Test public void testDiv() { Calculator cal = ac.getBean(Calculator.class);//Calculator類需要在spring容器才能使用aop //System.out.println(cal.div(3, 0)); System.out.println(cal.add(3, 2)); System.out.println(cal.divInteger(3, 0)); } }
測試結果
=============================================================== before方法:{com.lqy.spring.aop.Calculator.add}開始執行: 方法參數是:{[3.0, 2.0]} 加法運算 after方法:{com.lqy.spring.aop.Calculator.add}執行結束。 afterReturn方法:{com.lqy.spring.aop.Calculator.add}執行返回的結果是:{5.0}。 5.0 =============================================================== before方法:{com.lqy.spring.aop.Calculator.divInteger}開始執行: 方法參數是:{[3, 0]} 除法運算 after方法:{com.lqy.spring.aop.Calculator.divInteger}執行結束。 afterThrowing方法:{com.lqy.spring.aop.Calculator.divInteger}發生異常:java.lang.ArithmeticException: / by zero
(如果你覺得文章對你有幫助,歡迎捐贈,^_^,謝謝!)
================================
©Copyright 蕃薯耀 2020-01-21
https://www.cnblogs.com/fanshuyao/