Home Tutorial Spring Spring3 Aop Spting AOP Static Pointcut

 
 

Share on Google+Share on Google+
Spting AOP Static Pointcut
Posted on: September 4, 2010 at 12:00 AM
Advertisement
In this example you will learn how use static pointcut of Spring AOP.

Static Pointcut

Static pointcuts are based on method and target class, they cannot take into account the methods arguments.

SimpleBean.java

package roseindia.net.pointcut;

public class SimpleBean {
	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;
	}

}

SimplePointCut.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.StaticMethodMatcherPointcut;

public class SimplePointCut extends StaticMethodMatcherPointcut {

	@Override
	public boolean matches(Method method, Class clazz) {
		// TODO Auto-generated method stub
		return ("greetMethod".equals(method.getName()));
	}

}

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 SimplePointCut();
		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.greetMethod();

	}
}

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


Calling greetMethod
Have a Peaceful Day
proceed...........

Download this example code

Advertisement

Related Tags for Spting AOP Static 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
 

 

 

DMCA.com