Spring如何使用注解实现AOP配置呢?
下文笔者讲述Spring基于注解实现AOP配置的方法分享,如下所示
实现思路: 1.引入相应依赖 2.使用@Aspect和@Pointcut注解为其他方法添加依赖和切面 使用以上方法后,原方法就得到增强
Maven导入依赖
<dependencies> <!--spring上下文--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.18</version> </dependency> <!--测试--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.18</version> </dependency> <!--aop依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.18</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
Calculator配置信息
package com.java265.aop; import org.springframework.stereotype.Component; @Component public class Calculator { public int div(int num1, int num2) { System.out.println("Calculator --> div running"); return num1 / num2; } }
LogAspects配置信息
package com.java265.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import java.util.Arrays; @Aspect @Component public class LogAspects { /** * @Pointcut 切入点 * execution(* com.java265.aop.Calculator.*(..)) * 任意修饰的类 com.java265.aop.Calculator 下的所有方法和任意形参 */ @Pointcut("execution(* com.java265.aop.Calculator.*(..))") public void myPointcut() { } /** * @param joinPoint 连接点 * @Before 前置通知 */ @Before("myPointcut()") public void logStart(JoinPoint joinPoint) { //获取方法名 String name = joinPoint.getSignature().getName(); //获取传参 String args = Arrays.toString(joinPoint.getArgs()); System.out.println(name + "方法前置通知...参数信息" + args + ""); } /** * @param joinPoint 连接点 * @param result 返回结果 * @AfterReturning 返回通知 */ @AfterReturning(value = "myPointcut()", returning = "result") public void logReturn(JoinPoint joinPoint, Object result) { String name = joinPoint.getSignature().getName(); System.out.println(name + "方法返回通知...返回信息{" + result + "}"); } /** * @param joinPoint 连接点 * @param exception 异常信息 * @AfterThrowing 异常通知 */ @AfterThrowing(value = "myPointcut()", throwing = "exception") public void logException(JoinPoint joinPoint, Exception exception) { String name = joinPoint.getSignature().getName(); System.out.println(name + "方法异常通知...异常信息{" + exception + "}"); } /** * @param joinPoint 连接点 * @After 最终通知 */ @After("myPointcut()") public void logEnd(JoinPoint joinPoint) { String name = joinPoint.getSignature().getName(); System.out.println(name + "方法最终通知..."); } }
测试类配置信息
package com.java265.test; import com.java265.aop.Calculator; import com.java265.config.MainConfigAOP; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MainConfigAOP.class) public class AOPTest { @Autowired private Calculator calculator; @Test public void test01() { System.out.println("----------正常通知----------"); calculator.div(88, 10); System.out.println("\n---------- 异常通知----------"); calculator.div(99, 0); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。