aop 好像都需要目标类有接口类具体什么原理我不太清楚
程序结构如下
UserService如下
package com.qst.c_aspect_xml;
public class UserServerImpl implements UserService {
@Override
public void addUser() {
System.out.println("com.qst.c_aspect_xml UserServer addUser");
}
@Override
public String updateUser() {
System.out.println("com.qst.c_aspect_xml UserServer updateUser");
return "返回值";
}
@Override
public void deleteUser() {
System.out.println("com.qst.c_aspect_xml UserServer deleteUser");
}
}
创建切面类
通知有6类目前需要用五类:如下
aop联盟定义通知类型,具有特性接口,必须实现,从而确定方法名称。
aspectj 通知类型,只定义类型名称。已经方法格式。
个数:6种,知道5种,掌握1中。
before:前置通知(应用:各种校验)
在方法执行前执行,如果通知抛出异常,阻止方法运行
afterReturning:后置通知(应用:常规数据处理)
方法正常返回后执行,如果方法中抛出异常,通知无法执行
必须在方法执行后才执行,所以可以获得方法的返回值。
around:环绕通知(应用:十分强大,可以做任何事情)
方法执行前后分别执行,可以阻止方法的执行
必须手动执行目标方法
afterThrowing:抛出异常通知(应用:包装异常信息)
方法抛出异常后执行,如果方法没有抛出异常,无法执行
after:最终通知(应用:清理现场)
方法执行完毕后执行,无论方法中是否出现异常
package com.qst.c_aspect_xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/*
* 切面类有多个通知
*/
public class MyAspect {
public void myBefore(JoinPoint joinPoint) {
System.out.println("前置通知"+joinPoint.getSignature().getName());
}
public void myAfterReturning(JoinPoint joinPoint,Object ret) {
System.out.println("后置通知"+joinPoint.getSignature().getName()+"---->"+ret);
}
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("前置环绕");
//手动执行目标方法
Object obj = joinPoint.proceed();
System.out.println("后置环绕");
return obj;
}
//必须有参数
public void myAfterThrowing(JoinPoint joinPoint,Throwable e) {
System.out.println("抛出异常"+e.getLocalizedMessage());
}
public void myAfter() {
System.out.println("最终通知");
}
}
配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 创建bean工厂目标类 -->
<bean id="userServerImplId"
class="com.qst.c_aspect_xml.UserServerImpl"></bean>
<!-- 创建切面类 -->
<bean id="myAspectId" class="com.qst.c_aspect_xml.MyAspect"></bean>
<!-- <aop:aspect ref="myAspectId"></aop:aspect> 将切面类 声明为切面从而获得通知
<aop:pointcut expression="execution(* com.qst.c_aspect_xml.MyAspect.*(..) )" id="myPotintcut"/>
execution声明了一个切入点表达式所有的通知都可以使用 expression :切入点表达式 id 用去其他通知引用 -->
<aop:config>
<aop:aspect ref="myAspectId">
<aop:pointcut
expression="execution(* com.qst.c_aspect_xml.UserServerImpl.*(..) )"
id="myPotintcut" />
<!-- 前置通知 method 通知方法名 pointcut :切入点表达式,此表达式只能当前通知使用 pointcut-ref :切入点的引用
可以与其他通知共享切入点 二选一 通知方法可以有参数 通知方法的格式 public void myBefore(JoinPoint joinPoint)
{ System.out.println("前置通知"); } 参数一: 用于描述连接点(目标方法的方法名),获得目标方法的名称等 -->
<aop:before method="myBefore" pointcut-ref="myPotintcut" />
<!-- 后置通知 目标方法后执行 可以获得返回值 public void myAfterReturning(JoinPoint joinPoint,Object
ret) 参数一:用于描述连接点(目标方法的方法名),获得目标方法的名称等 参数二 类型必须是Object 参数是由returning 配置的 -->
<aop:after-returning method="myAfterReturning"
pointcut-ref="myPotintcut" returning="ret" />
<!-- 环绕通知 切面类格式public Object MyAround(ProceedingJoinPoint joinPoint) throws
Throwable{ 返回值类型必须是 Object 参数必须是ProceedingJoinPoint 需要抛出异常 执行目标方法 Object
obj = joinPoint.proceed(); -->
<aop:around method="myAround" pointcut-ref="myPotintcut" />
<!-- 抛出异常 格式:public void myAfterThrowing(JoinPoint joinPoint, Throwable
e){ 必须要有参数 参数一:连接点的描述对象 参数二 :获得异常信息的,类型是Throwable 参数明由 throwing="e"配置 throwing通知方法的第二个参数的名称 -->
<aop:after-throwing method="myAfterThrowing"
pointcut-ref="myPotintcut" throwing="e" />
<!-- 最终通知 无论如何都会执行 -->
<aop:after method="myAfter" pointcut-ref="myPotintcut" />
</aop:aspect>
</aop:config>
</beans>
此处着重学习execution(* com.qst.c_aspect_xml.MyAspect.*(…) ) 用法
execution确定切入点详解(https://blog.csdn.net/zz210891470/article/details/54175107)