介绍
xml方式AOP快速入门
1、导入AOP相关坐标
<!-- aop是一种思想,aspectj是一个小框架,内部实现了aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
2、准备目标类、通知,并配置给spring管理
3、配置切点表达式:哪些方法被增强
//需要引入 aop的命名空间
<!-- aop配置 -->
<aop:config>
<!-- 配置切点表达式,目的是指定哪些方法被增强,可以配置多个 -->
<!--
id:唯一标识
expression:配置切点
-->
<aop:pointcut id="myPointCut" expression="execution(void com.xxx.service.impl.UserServiceImpl.*())"/>
<!-- 配置织入,指定哪些切点与哪些通知进行结合 -->
<!-- pointcut-ref用于引用已经配置好的切点,也可以使用pointcut直接配置切点 -->
<aop:aspect ref="myAdvice">
<aop:before method="beforeAdvice" pointcut-ref="myPointCut"/>
<aop:after method="afterAdvice" pointcut="execution(void com.xxx.service.impl.UserServiceImpl.*()))"/>
</aop:aspect>
</aop:config>
4、配置织入:切点被哪些通知方法增强,是前置增强还是后置增强
xml方式AOP配置详解
节点表达式的配置语法
execution([访问修饰符] 包名.类名.方法名(参数))
访问修饰符可以省略
返回值类型、某一级包名、类名、方法名,可以使用 * 表示任意
包名与类名之间使用单点 . 表示该包下的类,使用双点 .. 表示该包及其子包下的类
参数列表可以使用两个点 .. 表示任意参数
通知的类型
前置通知:<aop:before> 目标方法执行之前执行
后置通知:<aop:after-returning> 目标方法执行之后执行,目标方法异常时,不在执行
环绕通知:<aop:around> 目标方法执行前后执行,目标方法异常时,环绕后方法不在执行
异常通知:<aop:after-throwing> 目标方法抛出异常时执行
最终通知:<aop:after> 不管目标方法是否有异常,最终都会执行
通知方法在被调用时,spring可以为其传递一些必要的参数
JoinPoint:连接点对象,任何通知都可以使用,可以获取当前目标对象、目标方法参数等信息
getArgs():获得目标方法参数
getTarget():获得目标对象
getStaticPart():获得精确的切点表达式信息
ProceedingJoinPoint:JoinPoint子类对象,主要是在环绕通知中执行 proceed(),进而执行目标方法
Throwable:异常对象,使用在异常通知中,需要在配置文件中指出异常对象名称
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointCut" throwing="异常参数名称" />
aop的配置的两种方式
使用<advisor>配置切面
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" />
不用指定通知的类型,但 MyAdvice类需要实现Advice中的接口,有实现的接口确定通知类型
使用<aspect>配置切面
实现:给计数器类中的方法添加日志
1、准备工作
1)添加依赖
<!--spring aspects依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.0.2</version>
</dependency>
2)准备 Calculator接口 和实现类 CalculatorImpl
包含加减乘除四个函数
2、创建切面类
@Component
public class LogAspect {
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}
......
}
3、在配置文件中配置
<aop:config>
<!--配置切面类-->
<aop:aspect ref="logAspect">
<aop:pointcut id="pointCut"
expression="execution(public int com.xxx.util.impl.CalculatorImpl.*(..))"/>
<aop:before method="beforeMethod" pointcut-ref="pointCut"></aop:before>
<aop:after method="afterMethod" pointcut-ref="pointCut"></aop:after>
<aop:after-returning method="afterReturningMethod" returning="result" pointcut-ref="pointCut"></aop:after-returning>
<aop:after-throwing method="afterThrowingMethod" throwing="ex" pointcut-ref="pointCut"></aop:after-throwing>
<aop:around method="aroundMethod" pointcut-ref="pointCut"></aop:around>
</aop:aspect>
</aop:config>
4、测试
@Test
public void myTest01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
Calculator calculator = ac.getBean( Calculator.class);
calculator.add(1, 1);
}
程序:
链接:https://pan.baidu.com/s/1LgxJi5PLiMrcAZpEUoBAZQ?pwd=vox3
原文始发于微信公众号(代码之旅):基于xml配置的AOP
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/203676.html