get handle of object intercepted by @Around annota

2019-08-21 07:50发布

问题:

getDescription method of Object of class Title was intercepted by an aspect. How do I get access to instance of object itself.

@Around("execution(String com.*.*.*.Title.getDescription(..))")
public String getInternationalizedTitleDescription(ProceedingJoinPoint joinPoint) throws Throwable {
    if (something){
        return joinPoint.proceed(); 
    } else {
        //here I need access to instance to Title
        //Title t = joinPoint.getObject();
        //return SomeOtherObject.getTitleData(t);
    }
}

回答1:

Use ProceedingJoinPoint#getTarget() or ProceedingJoinPoint#getThis() depending on which object you want.

getTarget()

Returns the target object. This will always be the same object as that matched by the target pointcut designator. Unless you specifically need this reflective access, you should use the target pointcut designator to get at this object for better static typing and performance.

getThis()

Returns the currently executing object. This will always be the same object as that matched by the this pointcut designator. Unless you specifically need this reflective access, you should use the this pointcut designator to get at this object for better static typing and performance.

Basically, this is the object that the method was invoked on (a proxy) and target is the proxied object.