================================
©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