Let’s say I have an aspect wrapper on all public methods of my services which detaches entities from database before returning them to controller:
@Around("execution(public * *(..)) && @within(org.springframework.stereotype.Service)")
When one service is calling another directly, this wrapper is being triggered as well. For instance:
@Service
class ServiceA {
@Autowired
ServiceB b;
public void foo() {
b.bar();
}
}
@Service
class ServiceB {
public void bar() {
}
}
When I call ServiceA.foo(), the wrapper is triggering around the nested call to bar() as well.
It should trigger around the call to foo(), but not bar(). How can I avoid this?
I’ve sometimes resolved this kind of problems using ThreadLocal variables. Try something like:
Obviously, this will only work when all calls are done in the same thread. Thread local variables have some other drawbacks too and is good to read about them.