记录实际存储库 class 而不是 JPARepository 的 CrudRepository

Log the actual repository class instead of CrudRepository of JPARepository

预注:真的很惊讶和恼火,为什么我找不到这个简单的东西。

public Object interceptMethod(ProceedingJoinPoint joinPoint) throws Throwable {
  joinPoint.getTarget(); //returns SimpleJpaRepository
  joinPoint.getSignature().getDeclaringTypeName(); //Returns CrudRepository
}

如何获取实际的存储库名称?

获取实际存储库名称的一种方法

@Before("execution(* org.sec3.jpa.bean.*.deleteById(*)) && target(bean)")
public void getRepositoryName(JoinPoint jp , Object bean ) throws Exception {
    Advised advised = (Advised) bean;
    for(Class<?> clazz : advised.getProxiedInterfaces())
    System.out.println(clazz);
}

将打印

interface org.sec3.jpa.bean.TestEmployeeRepository
interface org.springframework.data.repository.Repository
interface org.springframework.transaction.interceptor.TransactionalProxy

TestEmployeeRepository

package org.sec3.jpa.bean;

import org.springframework.data.repository.CrudRepository;

public interface TestEmployeeRepository extends JpaRepository<JpaEmployee, Long> {

}

我对类似问题的回答here