
Originally Posted by
marmot101
But the basic idea is: Spring AOP doesn't support interception from a function in the same class. It is definitely under this AOP category.
As I mentioned in my reply a couple of replies ago, Spring AOP *does* support interception from a function in the same class, you just need to do a bit more to get it to work.
The fundamental problem is that Spring uses a decorator, in the form of a runtime generated proxy to execute the interceptors (transactional, logging etc.), this effectively means that there are two classes, the generated proxy and your "target" class. The target class has no knowledge of the proxied class, and thus when it calls methods on itself it is completely bypassing the proxy. You can make the target class aware of the proxy by using the AopContext class (http://www.springframework.org/docs/...opContext.html).
To put it simply, Spring does the following:
Code:
public class MyClass {
public void myFirstMethod() {
mySecondMethod();
}
public void mySecondMethod() {
}
}
public class ProxyGeneratedBySpringWhichLooksLikeMyClass {
private final MyClass delegate;
private final List interceptors;
public ProxyGeneratedBySpringWhichLooksLikeMyClass (
MyClass myClass,
List interceptors
) {
this.delegate = myClass;
this.interceptors = interceptors;
}
public void myFirstMethod() {
runInterceptors();
delegate.myFirstMethod();
}
public void mySecondMethod() {
runInterceptors();
delegate.mySecondMethod()
}
}
The spring container will then do something like:
Code:
MyClass myClass = new ProxyGeneratedBySpringWhichLooksLikeMyClass(
new MyClass(),
interceptors);
So when clients call myClass.myFirstMethod() they are actually talking to the runtime generated proxy, however, if myFirstMethod() calls mySecondMethod() the call to mySecondMethod will *not* go through the proxy.
PLEASE(!!!) do not take the above code too seriously as Spring offers far more functionality, but hopefully it demonstrates the point.
Also note that this is completely irrelevant if you are using compile time weaving which doesn't use a decorator/proxy but modifies the byte code directly.