使用spring AOP触发了哪个接口的(扩展CrudRepository)删除方法?

时间:2019-11-18 13:29:52

标签: java spring spring-boot spring-data-jpa spring-aop

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

我正在从服务层调用userRepo.deleteById(1),并使用spring AOP,我想在调用任何deleteById时记录接口名称,以便我可以跟踪哪个接口的deleteById被触发了。我想要一个输出,可以为我提供接口名称的线索。

joinPoint.getSignature()返回通用名称,即void org.springframework.data.repository.CrudRepository.deleteById(Object),但我想查看UserRepository或调用了deleteById的任何存储库名称。

1 个答案:

答案 0 :(得分:1)

有什么帮助吗?

@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> {

}

更多详细信息:Manipulating Advised Objects