@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的任何存储库名称。

最佳答案

这会有所帮助吗?

@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

08-07 05:26