Using Spring AOP I am getting some data about my CrudRepository methods (Name of the method, parameters, time elapsed). But I would also need to add the query string to these data.

I have been investigating to try to intercept other methods using Spring AOP. I even see that there are alternatives for this as p6spy. Or the logs could be configured to show it, but it is not the solution that I think fits better.

This would be an example of the calls from which I want to get the information:

package com.test.example.server.persistence.mapping.api;

import com.test.example.server.model.mapping.Opponent;

public interface OpponentRepository extends CrudRepository<Opponent, Long> {
    Opponent findByUserId(Long userId);

Here I am using Spring AOP to intercept those calls, and get some information. I would also need to obtain the query string that is executed.

package com.test.example.persistence.logging;
public class Logging {

private static final String POINTCUT =
        "execution(* com.test.example.server.persistence.mapping.api..*(..)) ";

public void dataAccessMethods() {

public Object profile(ProceedingJoinPoint pjp) throws Throwable {
    long start = System.currentTimeMillis();
    Object output = pjp.proceed();
    long elapsedTime = System.currentTimeMillis() - start;
    StringBuilder args = new StringBuilder("[");
    for (Object arg : pjp.getArgs()) {

    System.out.println(pjp.getSignature().getName() + "," + args + "," + elapsedTime + "," +;
    //Here I would need to get the query string, and concatenate it to the previous string.
    //Later it will be modified to store this instead of printing it by console (The System.out is used only to test it).

    return output;

The ouput is:


But the expected output is:

    findByUserId,[4],25,"Select * from opponent where userId = 4",2019-04-29T12:31:55.565

0 Answers