NameMatchMethodPoincut Example in Spring AOP

The following example illustrates NameMatchMethodPointcut which is filters methods (to which the advice should be applied to) by name.
If you aren't clear about what is a pointcut, I recommend strongly to read understanding pointcut and advisor and come back here.

Theme of the application

The theme of the application is to illustrate NameMatchMethodPointcut. This application contains a SimpleBean which contains four methods.
  1. ding()
  2. ding(int)
  3. dong()
  4. dong(int)
Now, the theme is to apply advices for those methods with the name ding. You can do it by writing a static pointcut and filter the method in matches(). But you don't need to do that much. Spring provides a class called NameMatchMethodPointcut that filters methods by name. This class will have a method called addMethodName() that takes in the name of the method and filters it based on that. You can add as many method names you want. Now we will be applying the logging advice only for the ding(), ding(int) method.

Create a project in eclipse

  1. File -> New -> Project -> Java Project
  2. Give the project name spring42 and click Finish 
  3. Now, the project is created.
  4. Under the Project Explorer (in the sidebar) you will see spring42. If you aren't able to see Project Explorer, go to Window menu -> Show view -> Project Explorer.
  5. Right click on the spring42 and click Properties
  6. On the left side, click on Java Build Path.
  7. Select the Libraries tab and then click on Add External Jars
  8. Now, add these jar files starting with: spring-core, aop-alliance, spring-aop, commons-logging
  9. Click on OK and then you are read

To create a class

In the Project Explorer, navigate to spring42 and right click on spring42. Then go to New -> Class and give the name of the class as defined below.

Project structure

package spring42;

public class SimpleBean {

    public void ding()
        System.out.println("Just ding()");
    public void ding(int x)
    public void dong()
        System.out.println("Just dong()");
    public void dong(int x)

This is an around advice which will log a message before and after the method invocation.
package spring42;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoggingAdvice implements MethodInterceptor {

private static Log log;
    public Object invoke(MethodInvocation mi) throws Throwable
    Object val=null;
    // Get argument values
    Object[] args=mi.getArguments();
    String name=mi.getMethod().getName()+(args.length!=0?"("+args[0]+")":"()");
  "Invoking method "+name);
            // Invoke the method now
  "Method execution completed");
    return val;
} - Main class

This is the main class. In this class, we will create a NameMatchMethodPointcut object and add the method names to which the advice should be applied.
package spring42;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;

public class SpringPrg {

    public static void main(String args[])
        // Create target bean object
        SimpleBean sb=new SimpleBean();
        // Create a pointcut that filters methods by name
        NameMatchMethodPointcut pointcut=new NameMatchMethodPointcut();
        // The method to which the advice must be applied to
        // must be named ding
        // Create an advice
        Advice advice=new LoggingAdvice();
        // Create an advisor which is a combination
        // of pointcut and advice
        Advisor advisor=new DefaultPointcutAdvisor(pointcut,advice);
        // Create ProxyFactory
        ProxyFactory pf=new ProxyFactory();
        // Set the target bean object
        // Add the advisor
        SimpleBean bean=(SimpleBean)pf.getProxy();
        // Advice will be applied to ding()
        // Advice will not be applied to dong()


 Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Invoking method ding()
Just ding()
Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Method execution completed
Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Invoking method ding(10)
Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Method execution completed
Just dong()

As you can see from the above output, there is no information logged before dong() and dong(20)
Also see dynamic pointcut and static pointcut

Posted by at on

Tags: Spring AOP,