问题
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