简体   繁体   English

在 Spring 中使用 AspectJ 在映射器方法中捕获 setter

[英]Capture setters inside mapper method using AspectJ in Spring

I have java classes like this :我有这样的java类:

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

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

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.我的 FieldPermissionAspect 从用户的 db 获取权限字段映射,如果用户没有给定字段的权限,则将字段设置为 null。

I get a list of string field hierarchy like this :我得到这样的字符串字段层次结构列表:

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

I want to set b, c, d to null using @Around around their respective setters inside the fetchA() method.我想在 fetchA() 方法中使用 @Around 围绕它们各自的 setter 将 b、c、d 设置为 null。 Is it feasible using AspectJ and spring?使用 AspectJ 和 spring 是否可行? How do I access the setters for b, c, d inside the fetchA() method?如何在 fetchA() 方法中访问 b、c、d 的设置器?

I want to set b , c , d to null using @Around around their respective setters inside the fetchA() method.我想集bcdnull使用@Around内部各自的设置器周围fetchA()方法。 Is it feasible using AspectJ and spring?使用 AspectJ 和 spring 是否可行? How do I access the setters for b , c , d inside the fetchA() method?如何在fetchA()方法中访问bcd的设置器?

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.我的假设是,当(且仅当)从某个其他方法内部调用 setter 方法时,您希望拦截它们。 Ie you need a control-flow-dependent pointcut like cflow() or cflowbelow() .即你需要一个控制流相关的切入点,比如cflow()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.根据Spring 手册,Spring AOP 不支持这些切入点,但是您可以将 Spring 配置为使用带有 LTW(加载时编织)的完整AspectJ

For more details I suggest you show me yours ( MCVE , ideally on GitHub with Maven build), then I show you mine (concrete solution).有关更多详细信息,我建议您向我展示您的( MCVE ,最好在 GitHub 上使用 Maven 构建),然后我向您展示我的(具体解决方案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM