在 Spring 中使用 AspectJ 在映射器方法中捕获 setter [英] Capture setters inside mapper method using AspectJ in Spring

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

问题描述

我有这样的 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;
}

我有一个带有这样注释的映射器方法:

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 从用户的数据库中获取权限字段映射,如果用户没有给定字段的权限,则将字段设置为 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?

推荐答案

我想使用 @Aroundbcd 设置为 nullcode> 在 fetchA() 方法中围绕它们各自的 setter.使用 AspectJ 和 spring 是否可行?如何在 fetchA() 方法中访问 bcd 的 setter?

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 配置为使用完整的 AspectJ 与 LTW(加载时编织).

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 在映射器方法中捕获 setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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