Skip to content
Advertisement

Capture setters inside mapper method using AspectJ in Spring

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

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"]

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?

Advertisement

Answer

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?

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.

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement