ADS

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 sajian -> 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

 
 

SimpleBean.java

package spring42;

public class SimpleBean {

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

LoggingAdvice.java

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;
   
    static
    {
        log=LogFactory.getLog(LoggingAdvice.class);
    }
   
    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]+")":"()");
       
        if(log.isInfoEnabled())
        {
            log.info("Invoking method "+name);
           
            // Invoke the method now
            val=mi.proceed();
           
            log.info("Method execution completed");
        }
   
    return val;
    }
   
}

SpringPrg.java - 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;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;

public class SpringPrg {

    public static void main(String args[])
    {
        // Create sasaran 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
        pointcut.addMethodName("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 sasaran bean object
        pf.setTarget(sb);
      
        // Add the advisor
        pf.addAdvisor(advisor);
      
        SimpleBean bean=(SimpleBean)pf.getProxy();
      
        // Advice will be applied to ding()
        bean.ding();
        bean.ding(10);
      
        // Advice will not be applied to dong()
        bean.dong();
        bean.dong(20);
    }
   
}

Output

 
 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)
ding(10)
Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Method execution completed
Just dong()
dong(20)

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

Subscribe to receive free email updates:

ADS