为 Spring JPA 存储库方法配置切入点

问题描述 投票:0回答:2

我正在尝试创建一个在每次调用 Spring JpaRepository 的 save() 之后运行的方面。我将我的方面定义如下:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class ProcessAspect {

    @Autowired
    private ProcessService processService;

    @AfterReturning(value = "execution(* com.domain.control.repository.ProcessDao.save())))",
    returning = "result")
    private void propagateProcess(JoinPoint joinPoint, Object result) {
        log.info("Aspect is working, congratulations. Jointpoint {} , result {}", joinPoint, result.toString());
        Process process = (process) result;

       // do something on the object returned by save()
        processService.createOrUpdateProcess(process);
    }

}

我的存储库定义如下:

@Repository
public interface ProcessDao extends JpaRepository<Process, String>

如果我这样配置,那么该方面将不起作用。

如何配置我的方面以在通用 JPA 存储库方法之后运行?

java spring spring-aop
2个回答
4
投票

首先你的

ProcessDao
没有
save
方法,所以它不会匹配,其次你有一个带有无参数
save
方法的切入点,并且没有这样的东西。相反,您想在切入点和匹配 1 参数中使用 Spring 数据存储库类之一。

类似这样的事情

execution(* org.springframework.data.jpa.repository.JpaRepository+.save(..))))

或者使其更通用

execution(* org.springframework.data.repository.CrudRepository+.save(..))))

这应该使你的切入点匹配。还有一个

saveAll
saveAndFlush
,因此如果您还需要拦截这些切入点,您可能需要添加更多切入点。


0
投票

记录所有存储库的查询时间。

@Aspect
@Component
public class QueryTimeLogAspect {
    Logger logger = LoggerFactory.getLogger(QueryTimeLogAspect.class);

    @Pointcut("execution(public * org.springframework.data.repository.Repository+.*(..))")
    public void monitor() {

    }

    @Around("monitor()")
    public Object logQueryTime(ProceedingJoinPoint pjp) throws Throwable{
        long start = System.currentTimeMillis();
        Object output = null;
        try {
            output = pjp.proceed();
        } finally {
            Object[] args = pjp.getArgs();
            List<String> argsList = new ArrayList<String>();

            if(args != null){
                for( int i=0; i<args.length; i++){
                    Object arg = args[i];
                    if(arg != null) {
                        argsList.add(arg.getClass().getName());
                    }
                }
            }

            //logger.info(pjp.getTarget() + "." + pjp.getSignature() + " : Execution time: " + elapsedTime + " ms. ");
            String DBTime = String.format("DBTime className=%s, methodName=%s, arguments=%s, executionTime=%s, threadId=%s",
                    pjp.getTarget(),
                    pjp.getSignature(),
                    argsList,
                    System.currentTimeMillis() - start,
                    Thread.currentThread().getId());
            //if (System.currentTimeMillis() - start > 500) {
                logger.info("DBTime exceeds 500ms: " + DBTime);
            //}
        }
        return output;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.