Get class name and method parameters in the aspect

依然范特西╮ 提交于 2019-12-24 09:28:30

问题


I am working on a project that is basically a lot of processes that run periodically. Each process is a different class that extends an abstract class RunnableProcess we created, which contains the abstract method run with the signature below:

public abstract void run(Map processContext) throws IOException;

To improve modularization on the project, I'm starting to use Aspect Oriented Programming (AOP) to intercept the run calls from every RunnableProcess. I am still learning AOP, and I have the following code until now:

import static org.slf4j.LoggerFactory.getLogger;

import org.slf4j.Logger;
import process.RunnableProcess;
import java.util.Map;

public aspect ProcessRunInterceptorProtocol {

    pointcut runProcess() : call(void RunnableProcess.run(Map));

    before(): runProcess() {
        logger = getLogger(getClass());
        logger.info("running process " + thisJoinPoint);
    }

    after(): runProcess() {
        logger = getLogger(getClass());
        logger.info("process run successfuly " + thisJoinPoint);
    }

    private Logger logger;
}

The problem I'm having is related to the logger (org.slf4j.Logger) initialization - I would like it to be linked with the process class (the one that extended RunnableProcess, and it is being intercepted by the aspect), which is not happening here (the getClass() retrieves the aspect class). How can I do that without changing the implementation of RunnableProcess and its childs?


回答1:


You want the target object on which the method is executed. Try this:

logger = getLogger(thisJoinPoint.getTarget().getClass());


来源:https://stackoverflow.com/questions/48141023/get-class-name-and-method-parameters-in-the-aspect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!