3

Given the following example classes in my.package...

public class Foo {
    public void logicNotInBar()     {/*code*/}
    public void logicBarOverrides() {/*code*/}
}

public class Bar extends Foo {
    public void logicBarOverrides() {/*code*/}
}

and the following Spring-AOP pointcuts...

<aop:pointcut id="myPointcutAll" expression="execution(* my.package.*.*(..))"   />
<aop:pointcut id="myPointcutFoo" expression="execution(* my.package.Foo.*(..))" />
<aop:pointcut id="myPointcutBar" expression="execution(* my.package.Bar.*(..))" />

What is the result of advice applied to the above pointcuts on instances of Bar? In particular...

Bar bar = new Bar();
bar.logicNotInBar();      // will myPointcutBar advice trigger?
bar.logicBarOverrides();  // is myPointcutFoo ignored here?

I think I am missing some basic truth of how pointcuts interact with inheritance so an under-the-hood explanation/doc would probably go a long way.

Andrew White
  • 52,720
  • 19
  • 113
  • 137
  • 1
    No advice will be applied because Bar is not a "Proxy Bean", you can't manually instantiate a class that has to be advised... it has to be defined as Spring Bean: http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch08s06.html#aop-understanding-aop-proxies – davorp Oct 12 '11 at 10:20
  • and also it's good to use interfaces, and define Pointcuts on those interfaces rather then on implementations: http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch08.html#aop-introduction-proxies – davorp Oct 12 '11 at 10:34

1 Answers1

2

From aspectj documentation:

When matching method-execution join points, if the execution pointcut method signature specifies a declaring type, the pointcut will only match methods declared in that type, or methods that override methods declared in or inherited by that type. So the pointcut

execution(public void Middle.*())

picks out all method executions for public methods returning void and having no arguments that are either declared in, or inherited by, Middle, even if those methods are overridden in a subclass of Middle. So the pointcut would pick out the method-execution join point for Sub.m() in this code:

  class Super {
    protected void m() { ... }
  }
  class Middle extends Super {
  }
  class Sub extends Middle {
    public void m() { ... }
  }
davorp
  • 4,156
  • 3
  • 26
  • 34