Vikas Gupta: Software architect

AspectJ PointCut Expressions

Posted by Vikas Gupta on April 15, 2010

The intention of this blog is to explain AspectJ Pointcut Expressions in Spring Applications. In Aspect Oriented Programming, a pointcut is a set of joinpoints. A joinpoint is a point in program execution where you can add additional behavior. Spring applications only support method based joinpoints. So, you can use AspectJ pointcut expressions to define method pointcuts in Spring 2.x applications. Lets discuss some AspectJ pointcut expressions patterns.

Method Signature Patterns

The most typical pointcut expressions are used to match a number of methods by their signatures. A common method based pointcut expression is something like

expression(<method scope> <return type> <fully qualified class name>.*(parametes))
  1. method scope: Advice will be applied to all the methods having this scope. For e.g., public, private, etc. Please note that Spring AOP only supports advising public methods.
  2. return type: Advice will be applied to all the methods having this return type.
  3. fully qualified class name: Advice will be applied to all the methods of this type. If the class and advice are in the same package then package name is not required
  4. parameters: You can also filter the method names based on the types. Two dots(..) means any number and type of parameters.

Examples

  • execution(* com.aspects.pointcut.DemoClass.*(..)) : This advice will be applied to all the methods of DemoClass.
  • execution(* DemoClass.*(..)): You can omit the package if the DemoClass and the advice is in the same package.
  • execution(public * DemoClass.*(..)): This advice will be applied to the public methods of DemoClass.
  • execution(public int DemoClass.*(..)): This advice will be applied to the public methods of DemoClass and returning an int.
  • execution(public int DemoClass.*(int, ..)): This advice will be applied to the public methods of DemoClass and returning an int and having first parameter as int.
  • execution(public int DemoClass.*(int, int)): This advice will be applied to the public methods of DemoClass and returning an int and having both parameters as int.

Type Signature Patterns

These pointcut expressions are applied to all joinpoint of certain types. A common type signature patterns looks like

within(type name)  

Here type name is either the package name or the class name.

Examples

  • within(com.aspects.blog.package.*) : This will match all the methods in all classes of com.aspects.blog.package.
  • within(com.aspects.blog.package..*) : This will match all the methods in all classes of com.aspects.blog.package and its sub packages. The only difference is the extra dot(.) after package.
  • within(com.aspects.blog.package.DemoClass) : This will match all the methods in the DemoClass.
  • within(DemoClass) : Again, if the target class is located in the same package as this aspect, the package name can be omitted.
  • within(DemoInterface+) : This will match all the methods which are in classes which implement DemoInterface.

Bean Name Patterns

Spring 2.5 supports a new pointcut type that is used to match bean names. For example, the following pointcut expression matches beans whose name ends with Service.

bean(*Service) 

This pointcut is not supported by AspectJ annotation, hence you can declare them only in spring context files.

Combining Pointcut Expressions

Pointcut expressions can be combined using && (and), ||(or), and !(not). For example,

within(DemoInterface1+) || within(DemoInterface2+)

The above patterns will match all join point in all classes which implement DemoInterface1 or DemoInterface2

Declaring Pointcut Parameters

One way to access join point information is by reflection (i.e., via an argument of type org.aspectj.lang.JoinPoint in the advice method). Besides, you can access join point information in a declarative way by using some kinds of special pointcut expressions. For example,

@Aspect
public class SomeAspect {

  @Before(&quot;execution(* *.*(..)) &amp;&amp; target(target) &amp;&amp; args(a,b)&quot;)
  public void someMethod(Object target, int a, int b) {
    log.info(&quot;Target class : &quot; + target.getClass().getName());
    log.info(&quot;Arguments : &quot; + a + &quot;, &quot; + b);
  }
}

Here, target captures the target object and args captures the parameters.

Applying arbitrary patterns

Although the syntax of the AspectJ pointcut expressions is pretty rich, you may find some scenarios in which they are not sufficient to provide the necessary behaviour. In such scenarios, you may choose to create an annotation and use that to match joinpoints. This is done in the following manner

First create an annotation

@Target(ElementType.Method)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @Interface ApplyAspect {
}

Once you have created this annotation, you can created pointcut expressions like

@annotation(ApplyAspect)

This will match those methods which are annotated with @ApplyAspect. You can also apply this to classes, for example

@within(ApplyAspect)

This will match all joinpoints in classes annotated with @ApplyAspect.

Conclusion

In this blog, I have discussed the expressions for AspectJ pointcuts, which are powerful mechanism to filter the joinpoints.

Advertisements

17 Responses to “AspectJ PointCut Expressions”

  1. Good Work :). How do I include a specific method matching within any DAO class? If I were to look for xyz.abc.dao.*DAOImpl.insert* do I need to use Regex or is something else available ?

  2. Thankx. I was just missing some libraries :-D. I thought the new spring libraries messed up my older Spring usages…

  3. Patrick said

    Good blog Vikas. I have read a lot of AspectJ blogs and this explains execution patterns better than most.

  4. Suprashant said

    This blog informs in a simple, precise manner.
    Very useful blog.

    Thanks Vikas!

  5. Rajiv Malik said

    Hi Vikas, can i have some exclusion mechanism i.e to exclude a specific method, for eg:
    xy.abc.dao.*DAOImpl.save* . except this save method i would like pointcut to defined for all other methods

  6. Monika said

    Hi Vikas, Can we have abstract data types in return value in execution pointcut.I tried but it always throws error of unknown data type.

  7. Kamlesh said

    Good examples of different pointcuts. Thanks Vikas !!

    – Kamlesh
    http://www.kamleshkumar.com

  8. Good one. Thanks for sharing.

  9. Kirandeep Rana said

    Hi,
    I Had a Problem, I need to apply Advice to function let say foo(), but foo() is in different packages e.g. com.tt.abc.mm.foo() and com.tt.xyz.mm.foo()
    so joinpoint i have used is, execution(* com.tt..mm.foo(..))
    Note: double DOt(..) in package name.
    like this we can apply AOP to same function in different packages.
    Just wanted to share coz I spent much time to find it

  10. Pinky said

    Clear Explanation.. Thanks for sharing 🙂

  11. Nice blog… all complex configuration illustrated in very simple and easy way….
    Thanks

  12. Saurabh Agrawal said

    I just added the folloowing configuration in my spring xml file but I don’t see any logging happening.

    I am using hybris which is based on spring and when tomcat starts it gives me all sorts of exception in initializing other bean. Not sure how the above code is leading to BeanCreationException. Any thoughts ? Do I need to do anything additional to make my logging work.

    Exception trace:

    java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

  13. Reblogged this on HelpEzee.

  14. Spandana said

    Useful Blog 🙂

  15. Himanshi Pundir said

    How can I write a pointcut expression for only few methods starting with create(for ex : createQwe, createAbc)of a Class Xyz in some package a.b.c?
    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: