环绕通知
@Around
Proceedingjoinpoint 和JoinPoint的区别:
JoinPoint:aop的一个连接点,可以通过这个对象得到连接点的信息,比如它的目标对象
Proceedingjoinpoint 继承了JoinPoint,proceed()这个是aop代理链执行的方法。并扩充实现了proceed()方法,用于继续执行连接点。JoinPoint仅能获取相关参数,无法执行连接点。
JoinPoint的方法
1.java.lang.Object[] getArgs():获取连接点方法运行时的入参列表;
2.Signature getSignature() :获取连接点的方法签名对象;
3.java.lang.Object getTarget() :获取连接点所在的目标对象;
4.java.lang.Object getThis() :获取代理对象本身;
环绕通知意义
1.前后通知
2.如:
Object[] args = proceedingJoinPoint.getArgs();
if(args!=null&&args.length>0){
...
}
proceedingJoinPoint.proceed(args);
proceed()有重载,有个带参数的方法,可以修改目标方法的的参数
Introductions
官网原话
You can make an introduction by using the @DeclareParents annotation. This annotation is used to declare that matching types have a new parent (hence the name). For example, given an interface named UsageTracked and an implementation of that interface named DefaultUsageTracked, the following aspect declares that all implementors of service interfaces also implement the UsageTracked interface (to expose statistics via JMX for example):
我的理解,有错误欢迎指正
使用@DeclareParents声明匹配到的类型具有新的父类,例如,声明一个接口UsageTracked和接口的实现DefaultUsageTracked,@Before("com.xyz.myapp.SystemArchitecture.businessService() && this(usageTracked)")声明的服务接口的所有实现者也都实现该UsageTracked接口,而且用的是DefaultUsageTracked.class的实现
@Aspect
public class UsageTracking {
@DeclareParents(value="com.xzy.myapp.service.*+", defaultImpl=DefaultUsageTracked.class)
public static UsageTracked mixin;
@Before("com.xyz.myapp.SystemArchitecture.businessService() && this(usageTracked)")
public void recordUsage(UsageTracked usageTracked) {
usageTracked.incrementUseCount();
}
}
如:
UsageTracked usageTracked = (UsageTracked) context.getBean("xxx");
usageTracked 这个对象也实现了UsageTracked接口
perthis
@Aspect("perthis(com.xyz.myapp.SystemArchitecture.businessService())")
public class MyAspect {
private int someState;
@Before(com.xyz.myapp.SystemArchitecture.businessService())
public void recordServiceUsage() {
// ...
}
}
官网原话
In the preceding example, the effect of the 'perthis' clause is that one aspect instance is created for each unique service object that executes a business service (each unique object bound to 'this' at join points matched by the pointcut expression). The aspect instance is created the first time that a method is invoked on the service object. The aspect goes out of scope when the service object goes out of scope. Before the aspect instance is created, none of the advice within it executes. As soon as the aspect instance has been created, the advice declared within it executes at matched join points, but only when the service object is the one with which this aspect is associated. See the AspectJ Programming Guide for more information on per clauses.
我的理解
当两个不同的对象有相同的切面会有矛盾,又不想全部都是prototype,所以用这个perthis
perthis和pertarget区别
The pertarget instantiation model works in exactly the same way as perthis, but it creates one aspect instance for each unique target object at matched join points.
本文深入探讨了AOP(面向切面编程)的核心概念,包括JoinPoint、ProceedingJoinPoint的区别与应用,环绕通知的实现方式及意义。通过具体代码示例解析了@DeclareParents注解如何引入新的父类实现,以及perthis和pertarget实例化模型的工作原理。
3113

被折叠的 条评论
为什么被折叠?



