4

I am using Spring 3.2 and AspectJ 1.7.1. (It is not likely that I can upgrade to later versions in the near future.)

I need to define a pointcut for a protected method in an abstract class. AFAIK I need AspectJ for methods that are not public, so I have only tried this with the (AspectJ) annotations:

package com.aspects;
@Aspect
public class Aspect{

    @Before("execution(* com.x.y.x.MyClass.myMethod(..))")
    public void beforeAspect(){
       //do something here
    }

}

In my beans.xml I have:

<aop:aspectj-autoproxy />
<bean id="myAspect" class="com.aspects.Aspect"/>

I have checked and my Aspect class is created (constructor is getting called), no exception is being thrown when the application is launched. However I can not get the beforeAspect to be called. For public methods in non abstract classes this works. How can I make it work for protected method in abstract class?

Atticus
  • 1,622
  • 7
  • 32
  • 55

2 Answers2

9

You should add a + sign after the abstract class.

So

"execution(* com.x.y.x.MyClass.myMethod(..))" 

should look like:

"execution(* com.x.y.x.MyClass+.myMethod(..))"
                              ↑

The + is about inheritance either extending the given class (MyClass) or implementing an interface.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2

Actually your are using @AspectJ-style to configure Spring AOP. the <aop:aspectj-autoproxy> declares an AnnotationAwareAspectJAutoProxyCreator BeanPostProcessor but you need to run the aspectj compiler instead.

See 9.8 Using AspectJ with Spring applications in reference documentantion for more info about weaving aspects with Spring.

See Spring / @Transactional with AspectJ is totally ignored for more info about configure compile time weaving with maven.

Community
  • 1
  • 1
Jose Luis Martin
  • 10,459
  • 1
  • 37
  • 38
  • Thanks for the hints. I am reading the documentation. I did not know I am not using the `AspectJ` compiler. – Atticus Jan 09 '14 at 12:02