Which Interface's (extending CrudRepository) delete method was triggered using spring AOP?

扶醉桌前 提交于 2019-12-23 03:48:11

问题


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

I am calling userRepo.deleteById(1) from my service layer and using spring AOP I want to log the Interface name whenever any deleteById is called so that I can track which interface's deleteById was triggered. I want an output which can give me a clue of the interface name.

joinPoint.getSignature() returns the generic name i.e. void org.springframework.data.repository.CrudRepository.deleteById(Object) and but I want to see UserRepository or any repository name whose deleteById was called.


回答1:


Will this help ?

@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);
}

prints

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

TestEmployeeRepository is as follows

package org.sec3.jpa.bean;

import org.springframework.data.repository.CrudRepository;

public interface TestEmployeeRepository extends JpaRepository<JpaEmployee, Long> {

}

More details : Manipulating Advised Objects



来源:https://stackoverflow.com/questions/58915717/which-interfaces-extending-crudrepository-delete-method-was-triggered-using-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!