Home Tutorial Spring Spring3 Aop Spring AOP Dynamic Pointcut


Share on Google+Share on Google+
Spring AOP Dynamic Pointcut
Posted on: September 4, 2010 at 12:00 AM
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.


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");


package roseindia.net.pointcut;

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

public class SimpleAdvice implements MethodInterceptor {

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



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()));

	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);


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();
		beanProxy = (SimpleBean) proxyFactory.getProxy();


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
Dynamic Cheching for helloMethod
Calling helloMethod
Hello method called...15
Have a Peaceful Day

Download this example code


Related Tags for Spring AOP Dynamic Pointcut:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 4, 2010

Recommend the tutorial

Advertisements Advertisements