mardi 27 août 2019

In Spring AOP, what is the difference between joinPoint.proceed and method.invoke?

I don't know the difference between the two methods, because they all work very well.

The reason I tried to use method.invoke is that I want to call other methods instead in the aspect.

@Component
public class DefaultApiInvokeFacadeImpl implement ApiInvokeFacade {
    @Override
    public ResponseDTO request(RequestDTO request,Class bodyClazz) {
        return request(request,bodyClazz,null);
    }
    @Override
    public ResponseDTO request(RequestDTO request,Class bodyClazz,InvokeContext invokeContext) {
        // .... some codes
        return response;
    }
}

@Component
@Aspect
public class HttpInvokeAspect {

    @Pointcut("execution(" +
            "public io.github.lvyahui.http.api.model.ResponseDTO io.github.lvyahui.http.api.facade.impl.DefaultApiInvokeFacadeImpl" +
            ".request(..)" +
            ")")
    public void httpApi() {
    }

    @Around("httpApi()")
    public Object arroud(ProceedingJoinPoint joinPoint) throws Throwable {
        Object [] args = Arrays.copyOf(joinPoint.getArgs(),3);
        Method method = joinPoint.getSignature().getDeclaringType().getDeclaredMethod("request", RequestDTO.class,
                    Class.class, InvokeContext.class)
        // A
        /// Object ret = joinPoint.proceed(args);
        // B
        Object ret = method.invoke(joinPoint.getTarget(),args);
        return ret;
    }
}





Aucun commentaire:

Enregistrer un commentaire