Create Pointcut using simple name matching

February 20, 2018

You can create the Pointcut by simply matching the name of the method. We can use the NameMatchMethodPointcut class for this. In this example when you match “drive” for drive() and drive(speed). Spring does not care about the argument

We will start withe the class Car

package com.kayak;


import org.springframework.stereotype.Component;

public class Car  {
   
    private String name;

    public void setName(String name) {
        this.name = name;
    }
   
    public void getName() {
        System.out.println("Audi");
    }
    
    public void drive(int speed){
        System.out.println("Driving at given speed");
    }
    
    public void drive(){
        System.out.println("Driving");
    }
    
}

Then you have the CarAdvice class with before method

package com.kayak;

import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;

public class CarAdvice implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] os, Object o) throws Throwable {
      
      System.out.println("Inside before Advice");
    }

    
}

You can test the code with following code. You can create the NameMatchMethodPointcut and use addMethodName() to add the name of the method you want to match


package com.kayak;
import org.springframework.aop.Advisor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;

public class Test {
    public static void main(String[] args) {
        
        
          Car target= new Car();
          NameMatchMethodPointcut nameMatchPointcut = new NameMatchMethodPointcut();
          nameMatchPointcut.addMethodName("drive");
          
          CarAdvice cd= new CarAdvice();
          Advisor advisor= new DefaultPointcutAdvisor(nameMatchPointcut,cd );
          
          ProxyFactory pf=new ProxyFactory();
          pf.setTarget(target);
          pf.addAdvisor(advisor);
          
          
          // Get the proxied bean object
          Car carProxy=(Car)pf.getProxy();
          carProxy.drive(40);
          carProxy.getName();
          
    }
    
}


You can use this pom.xml file to get dependencies for this project



    4.0.0
    com.codekayak
    SimpleNameMatching
    1.0-SNAPSHOT
    jar
    
        UTF-8
        1.8
        1.8
    
    
    

		
			org.springframework
			spring-core
			4.3.9.RELEASE
		

		
			org.springframework
			spring-context
			4.3.9.RELEASE
		

		
		
			org.springframework
			spring-aop
			4.3.9.RELEASE
		

		
			org.aspectj
			aspectjrt
			1.8.13
		

		
			org.aspectj
			aspectjweaver
			1.8.13
		

	
    

Now you can get the following output when you run the application

--- exec-maven-plugin:1.2.1:exec (default-cli) @ SimpleNameMatching ---
Inside before Advice
Driving at given speed
Inside before Advice
Driving
Audi