如何使用 spring AOP 拦截所有具有 @Repository 注释的存储库 类
How to intercept all the repository classes which has @Repository annotation using spring AOP
我正试图从我的 BaseRepostitary
文件中拦截所有返回的列表。这样我就可以在列表中找到必须使用此 decrypt
方法解密的名称。以下是我的看点Class
@Aspect
@Service
public class DecryptionAspect {
@AfterReturning(value = "execution(java.util.List *(..)) "
+ "&& target(org.springframework.stereotype.Repository)) ", returning = "list")
public void decrypt(List list) throws Exception
{
//Do decryption here for the names inside the list
}
}
但问题是这个 decrypt
方法在我的存储库 classes 被命中时没有触发。所以我的表达有问题。我知道我可以通过包名称定位存储库 class。但是我有很多存储库 classes 并且我已经为那个 classes 提供了 @Repository
注释。所以我希望这个 AOP 表达式能够识别哪些 classes 具有 @Repository
注释,并拦截存储库 classes 中的所有列表项。那么如何重写我的表情。提前致谢!
`@Aspect
@Service
public class DecryptionAspect {
@AfterReturning(value = "execution(java.util.List *(..)) "
+ "&& @target(org.springframework.stereotype.Repository)) ", returning = "list")
public void decrypt(List list) throws Exception
{
//Do decryption here for the names inside the list
}
}`
您应该使用 @target
将目标 class 与特定注释匹配。
BaseRepostitary class 是否实现了一些接口?
我尝试 this.Hope 有效。
@Aspect
@Service
public class TestAop {
@AfterReturning(value = "execution( java.util.List *.queryList*(..)) "
+ " && @target(org.springframework.stereotype.Repository)", returning = "list")
public void decrypt(List list) throws Exception
{
System.out.println("test........."+ list.toString());
//Do decryption here for the names inside the list
}
}
服务实现:
@Repository
public class ServiceImpl implements IService {
@Override
public <T> List<T> queryList(Map<String, Object> paramMap, Class<T> clazz) {
return null;
}
}
终于明白了!
@AfterReturning(value="execution(* *..*Repository.*(..))",returning="list")
public void decrypt(List list) throws Exception
{
//Do decryption here for the names inside the list
}
}
我正试图从我的 BaseRepostitary
文件中拦截所有返回的列表。这样我就可以在列表中找到必须使用此 decrypt
方法解密的名称。以下是我的看点Class
@Aspect
@Service
public class DecryptionAspect {
@AfterReturning(value = "execution(java.util.List *(..)) "
+ "&& target(org.springframework.stereotype.Repository)) ", returning = "list")
public void decrypt(List list) throws Exception
{
//Do decryption here for the names inside the list
}
}
但问题是这个 decrypt
方法在我的存储库 classes 被命中时没有触发。所以我的表达有问题。我知道我可以通过包名称定位存储库 class。但是我有很多存储库 classes 并且我已经为那个 classes 提供了 @Repository
注释。所以我希望这个 AOP 表达式能够识别哪些 classes 具有 @Repository
注释,并拦截存储库 classes 中的所有列表项。那么如何重写我的表情。提前致谢!
`@Aspect
@Service
public class DecryptionAspect {
@AfterReturning(value = "execution(java.util.List *(..)) "
+ "&& @target(org.springframework.stereotype.Repository)) ", returning = "list")
public void decrypt(List list) throws Exception
{
//Do decryption here for the names inside the list
}
}`
您应该使用 @target
将目标 class 与特定注释匹配。
BaseRepostitary class 是否实现了一些接口?
我尝试 this.Hope 有效。
@Aspect
@Service
public class TestAop {
@AfterReturning(value = "execution( java.util.List *.queryList*(..)) "
+ " && @target(org.springframework.stereotype.Repository)", returning = "list")
public void decrypt(List list) throws Exception
{
System.out.println("test........."+ list.toString());
//Do decryption here for the names inside the list
}
}
服务实现:
@Repository
public class ServiceImpl implements IService {
@Override
public <T> List<T> queryList(Map<String, Object> paramMap, Class<T> clazz) {
return null;
}
}
终于明白了!
@AfterReturning(value="execution(* *..*Repository.*(..))",returning="list")
public void decrypt(List list) throws Exception
{
//Do decryption here for the names inside the list
}
}