1、注解
@Before:前置通知,在方法执行之前执行
@After:后置通知,在方法执行完成之后执行
@AfterReturing:返回通知:在返回结果之后运用
@AfterThrowing:异常通知,出现异常的时候使用
@Around:环绕通知
正常执行顺序 :
正常执行(无异常):@Before --> @After --> @AfterReturning
有异常:@Before --> @After --> @AfterThrowing
2、导入pom依赖
<!--context:有关于bean引用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<!--AspectJ支持-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<!--Spring动态代理cglib-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
3、aop注解的引用实例
(1)切面类
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogUtil {
@Before("execution(public Integer com.msb.service.MyCalculcator.add(Integer,Integer))")
public static void start(){
System.out.println("sub方法开始执行:参数是");
}
@AfterReturning("execution(public Integer com.msb.service.MyCalculcator.add(Integer,Integer))")
public static void stop(){
System.out.println("方法执行结束,结果是");
}
@AfterThrowing("execution(public Integer com.msb.service.MyCalculcator.add(Integer,Integer))")
public static void logException(){
System.out.println("方法抛出异常"); //getMessage():异常对象的消息字符串
}
@After("execution(public Integer com.msb.service.MyCalculcator.add(Integer,Integer))")
public static void logFinally(){
System.out.println("方法执行结束");
}
}
这里每个注解后面的“execution”属性都跟着方法的具体返回值类型、包具体地址以及方法参数的类型,而只能调用所引用类的一个方法,实际使用肯定不会这样子使用,这里可以用通配符“*”、通配符“.”。
通配符“*”的应用:
a:可以直接使用代替方法名称,这样子就可以引用类中全部方法了,也就是“”可以用来匹配一个或多个字符。
@Before("execution(public Integer com.msb.service.MyCalculcator.*(Integer,Integer))")
b:匹配任意类型的参数
@Before("execution(public Integer com.msb.service.MyCalculcator.*(Integer,*))")
c:只能匹配一层路径,不可匹配多层,也就是可以用来代替某一字符,但是最终只能匹配一个路径的类
d:不确定修饰符是什么时,可以省略不写,但不能使用代替
@Before("execution(Integer com.msb.service.MyCalculcator.*(Integer,*))")
通配符“.”的应用:
a:可以匹配多个参数,任意类型
@Before("execution(Integer com.msb.service.MyCalculcator.*(..))")
b:可以匹配多层路径
//".."代表中间的路径位置,第一个“*”代表类名称后缀,从而实现匹配多个类,也就是多个路径。
@Before("execution(Integer com.msb..MyCalculcator*.*(..))")
因此通过通配符可以写到特别简便,但是有时候并不是越简便越好。
另外,使用表达式的时候还支持逻辑运算符(&&、||、!)
execution("* *(..)")
上面的切面类是没有返回值、方法名称及方法参数的,若要获取,需要调用JointPoin对象,可以通过对象的方法获取。
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogUtil {
@AfterReturning(value = "execution(public Integer com.msb.service.MyCalculcator.add(Integer,Integer))",returning = "result")
public static void stop(JoinPoint joinPoint,Object result){
System.out.println("方法执行结束,结果是"+result);
}
@AfterThrowing("execution(public Integer com.msb.service.MyCalculcator.add(Integer,Integer))")
public static void logException(){
System.out.println("方法抛出异常"); //getMessage():异常对象的消息字符串
}
/**
* @Before:前置通知,在方法执行之前执行
* @After:后置通知,在方法执行完成之后执行
* @AfterReturing:返回通知:在返回结果之后运行
* @AfterThrowing:异常通知,出现异常的时候使用
* @Around:环绕通知
*/
/**
* 多个方法的表达式一样,可以自定义一个无参构造方法,
* 定义后添加@PointCut,后续使用可以直接调用
* 此处的方法只是起到一个声明作用,能够提供内部其他通知方法的调用
*/
@Pointcut("execution(Integer com.msb.service.MyCalculcator.*(..))")
public void myPointCut(){ }
/**
* JoinPoint对象,可通过对象方法获取方法名称、参数等
* 获取返回值:需要在注解的表达式添加"Returning"属性,属性名称必须要跟下面方法对应
* 获取异常信息:需要在注解添加“Throwing”属性,属性名称必须要跟下面方法对应
* 若要添加其他参数,需要在execution中添加args(参数列表),且加多参数argsName("参数列表")
* @Before(value = "execution(Integer com.msb.service.MyCalculcator.*(..)) && args(joinPoint,i)",argNames = "joinPoint,i")
*/
@Before(value = "myPointCut()")
public static void start(JoinPoint joinPoint) {
//获取方法名
Signature signature = joinPoint.getSignature();
//获取参数信息
Object[] args = joinPoint.getArgs();
System.out.println(signature.getName()+"sub方法开始执行:参数是");
}
@After("execution(public Integer com.msb.service.MyCalculcator.add(Integer,Integer))")
public static void logFinally(){
System.out.println("方法执行结束");
}
}
(2)appcationContext.xml配置
在使用时,同样需要在xml中配置包的扫描,另外需要开启aop的注解功能。
<!--开启包的扫描-->
<context:component-scan base-package="com.msb"></context:component-scan>
<!--开启aop的注解功能-->
<!--这里加了属性“proxy-target-class="true"是因为包中包含接口,而创建的bean是到具体的实现类,所以需要将这里写为”-->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
(3)测试类
@Test
public void test02() throws NoSuchMethodException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//这里调用的是接口的bean,使用接口的bean,默认是使用jdk中的Proxy动态代理类
Calculator calculator = context.getBean(Calculator.class);
calculator.add(1,2);
System.out.println(calculator.getClass());
//这里使用的是实体类的bean,默认使用的是Spring中的cglib动态代理,使用这个时在开启aop注解需要加属性:proxy-target-class="true"
MyCalculcator myCalculcator = context.getBean("myCalculcator", MyCalculcator.class);
myCalculcator.add(1,2);
System.out.println(myCalculcator.getClass());
}
后续看AOP注解2:https://blog.csdn.net/weixin_44975592/article/details/122649730