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