spring散记(三)

本文深入探讨了AOP(面向切面编程)的核心概念,包括JoinPoint、ProceedingJoinPoint的区别与应用,环绕通知的实现方式及意义。通过具体代码示例解析了@DeclareParents注解如何引入新的父类实现,以及perthis和pertarget实例化模型的工作原理。

环绕通知

@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.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值