如何使用Spring AOP拦截所有具有@Repository批注的存储库类

时间:2018-12-03 13:47:39

标签: java spring aop

我正在尝试拦截CameraCharascteristics文件中所有返回的列表。这样我可以在列表中找到必须使用此BaseRepostitary方法解密的名称。以下是我的Aspect类

decrypt

但是问题是,在我的存储库类被命中时,此@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 } } 方法没有触发。所以我的表情有毛病。我知道我可以通过包名称来定位Repository类。但是我有许多存储库类,并且为这些类提供了decrypt批注。因此,我希望此AOP表达式能够识别出哪些类具有@Repository注释,并拦截存储库类中的所有List项。那么如何重写我的表情。预先感谢!

2 个答案:

答案 0 :(得分:0)

`@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将目标类别与特定注释进行匹配。

BaseRepostitary类是否实现某些接口?

我尝试这个。希望能奏效。

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

}

ServiceImpl:

 @Repository
 public class ServiceImpl  implements IService {
   @Override
 public <T> List<T> queryList(Map<String, Object> paramMap, Class<T> clazz) {
    return null;
}

}

答案 1 :(得分:0)

最后我明白了!

@AfterReturning(value="execution(* *..*Repository.*(..))",returning="list")
  public void decrypt(List list) throws Exception
    {
        //Do decryption here for the names inside the list
    }

}