在Spring中使用AspectJ在mapper方法内捕获设置器 [英] Capture setters inside mapper method using AspectJ in Spring

查看:171
本文介绍了在Spring中使用AspectJ在mapper方法内捕获设置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的java类:

I have java classes like this :

@Data
public class Lead {
    private A a;
    ...
}

@Data
public class A {
    private B b;
    private String c;
    private List<Integer> d;
}

@Data 
public class B {
    private String e;
    private String f;
}

我有一个带有这样注释的mapper方法:

I have a mapper method with annotation like this :

@FieldPermissionAnnotation("a")
public A fetchA(//Some DB Entities) {
    A a = new A();
    ...
    a.setB(fetchB());
    ...
    a.setC(fetchC());
    ...
    a.setD(fetchD());
}

我的FieldPermissionAspect从db获取用户的权限字段映射,如果用户没有给定字段的权限,则将字段设置为null.

My FieldPermissionAspect fetches the permission-field mapping from db for a user and sets field to null if user does not have permission for given field.

我得到一个像这样的字符串字段层次结构的列表:

I get a list of string field hierarchy like this :

["a-b-e", "a-b-f", "a-c", "a-d"]

我想在fetchA()方法中使用@Around将它们各自的setter设置为b,c,d为null. 使用AspectJ和spring可行吗? 如何在fetchA()方法中访问b,c,d的设置器?

I want to set b, c, d to null using @Around around their respective setters inside the fetchA() method. Is it feasible using AspectJ and spring? How do I access the setters for b, c, d inside the fetchA() method?

推荐答案

我想使用@Around围绕fetchA()方法内各自的设置器,将bcd设置为null.使用AspectJ和spring可行吗?如何在fetchA()方法内访问bcd的设置器?

I want to set b, c, d to null using @Around around their respective setters inside the fetchA() method. Is it feasible using AspectJ and spring? How do I access the setters for b, c, d inside the fetchA() method?

正如我在评论中所说,您的问题不清楚,由于没有方面代码,我不得不猜测您想做什么.我的假设是,当(且仅当)从某个其他方法内部调用setter方法时,您想拦截它们. IE.您需要一个与控制流相关的切入点,例如cflow()cflowbelow().根据 Spring手册 Spring AOP不支持这些切入点,但是您可以将Spring配置为使用完整的

As I said in my comment, your question is unclear and I have to guess what you want to do because there is no aspect code. My assumption is that you want to intercept setter methods if (and only if) they are being called from inside a certain other method. I.e. you need a control-flow-dependent pointcut like cflow() or cflowbelow(). According to the Spring manual these pointcuts are not supported by Spring AOP, but you can configure Spring to use full AspectJ with LTW (load-time weaving) instead.

有关更多详细信息,我建议您向我展示您的( MCVE ,最好在GitHub上使用Maven构建),然后再向您展示我的(具体解决方案).

For more details I suggest you show me yours (MCVE, ideally on GitHub with Maven build), then I show you mine (concrete solution).

这篇关于在Spring中使用AspectJ在mapper方法内捕获设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆