Home Tutorial Spring Spring3 Aop NameMatchMethodPointcut Examples

 
 

Share on Google+Share on Google+
NameMatchMethodPointcut Examples
Posted on: September 8, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn how to use NameMatchMethodPointcut class in pointcut to match the methods names.

NameMatchMethodPointcut Examples

This NameMatchMethodPointcut matches for simple method name, its alternative is regexp patterns. Does not handle overloaded methods, in this all methods *with a given name will be eligible.

SimpleBean.java

package roseindia.net.bean;

public class SimpleBean {
	public void sayHi() {
		System.out.println("Hi Friend");
	}

	public void sayHi(String name) {
		System.out.println("Hi " + name);
	}

	public void greet() {
		System.out.println("Have a Nice Day......");
	}
}

SimpleAdvice.java

package roseindia.net.advice;

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("Proceed " + methodInvocation.getMethod().getName()
				+ "()");
		Object object = methodInvocation.proceed();
		System.out.println("Method End");
		return object;
	}

}

MainClaz.java

package roseindia.net.main;

import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;

import roseindia.net.advice.SimpleAdvice;
import roseindia.net.bean.SimpleBean;

public class MainClaz {
	public static void main(String[] args) {
		SimpleBean simpleBean = new SimpleBean();
		NameMatchMethodPointcut nameMatchMethodPointcut = new NameMatchMethodPointcut();
		nameMatchMethodPointcut.addMethodName("sayHi");
		nameMatchMethodPointcut.addMethodName("greet");
		SimpleAdvice simpleAdviceadvice = new SimpleAdvice();
		Advisor advisor = new DefaultPointcutAdvisor(nameMatchMethodPointcut,
				simpleAdviceadvice);

		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.addAdvisor(advisor);
		proxyFactory.setTarget(simpleBean);

		SimpleBean proxyBean = (SimpleBean) proxyFactory.getProxy();

		proxyBean.sayHi("Vinay");
		proxyBean.sayHi();
		proxyBean.greet();

	}
}

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


Proceed sayHi()
Hi Vinay
Method End
Proceed sayHi()
Hi Friend
Method End
Proceed greet()
Have a Nice Day......
Method End

Download this example code

Advertisement

Related Tags for NameMatchMethodPointcut Examples:


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 8, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com