Spring AOP Dynamic Pointcut


 

Spring AOP Dynamic Pointcut

In this example you will learn how use Dynamic pointcut of Spring AOP.

In this example you will learn how use Dynamic pointcut of Spring AOP.

Dynamic Pointcut

Dynamic Pointcuts takes method arguments, as well as static information. that is they must be evaluated with every method invocation.

SimpleBean.java

package roseindia.net.pointcut;

public class SimpleBean {
	public void helloMethod(int intVal) {
		System.out.println(" Hello method called..." + intVal);
	}

	public void greetMethod() {
		System.out.println("Have a Peaceful Day");
	}
}

SimpleAdvice.java

package roseindia.net.pointcut;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class SimpleAdvice implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("Calling " + methodInvocation.getMethod().getName());
		Object obj = methodInvocation.proceed();
		System.out.println("proceed...........");
		return obj;
	}

}

DynamicPointcutExample.java

package roseindia.net.pointcut;

import java.lang.reflect.*;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.ClassFilters;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.jms.support.destination.DynamicDestinationResolver;

public class DynamicPointcutExample extends DynamicMethodMatcherPointcut {

	public boolean matches(Method method, Class claz) {
		System.out.println("Static Checking " + method.getName() + " method");
		return ("helloMethod".equals(method.getName()));
	}

	@Override
	public boolean matches(Method method, Class clazz, Object[] objects) {
		// TODO Auto-generated method stub
		System.out.println("Dynamic Cheching for " + method.getName());
		int testVal = ((Integer) objects[0]).intValue();
		return (testVal != 50);
	}
}

TestPointCut.java

package roseindia.net.pointcut;

import java.lang.reflect.Proxy;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.Pointcuts;

public class TestPointCut {
	public static void main(String[] args) {
		SimpleBean simpleBean = new SimpleBean();
		SimpleBean beanProxy;
		Pointcut pointcut = new DynamicPointcutExample();
		Advice advice = new SimpleAdvice();
		Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);

		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.addAdvisor(advisor);
		proxyFactory.setTarget(simpleBean);
		beanProxy = (SimpleBean) proxyFactory.getProxy();
		beanProxy.helloMethod(1);
		beanProxy.helloMethod(15);
		beanProxy.greetMethod();

	}
}

When you run this application it will display message as shown below:


Static Checking helloMethod method
Static Checking greetMethod method
Static Checking toString method
Static Checking hashCode method
Static Checking helloMethod method
Dynamic Cheching for helloMethod
Calling helloMethod
Hello method called...1
proceed...........
Dynamic Cheching for helloMethod
Calling helloMethod
Hello method called...15
proceed...........
Have a Peaceful Day

Download this example code

Ads