Share on Google+Share on Google+

Composable Pointcut - Union

Advertisement
In this tutorial you will learn about Union operation using ComposablePointcut.

ComposablePointcut- Union Operation

A composable pointcuts can be composed using the static methods in the org.springframework.aop.support.Pointcuts class or using composable pointcut class in the same package.

MainClaz.java

package roseindia.net.union;

import java.lang.reflect.Method;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcher;

public class MainClaz {
	ComposablePointcut composablePointcut = new ComposablePointcut(
			ClassFilter.TRUE);

	public static void test(SimpleBean proxy) {
		proxy.getAge();
		proxy.setName("Vinay");
		proxy.getName();
	}

	public static SimpleBean getProxy(ComposablePointcut composablePointcut,
			SimpleBean simpleBean) {
		Advice advice = new TestAdvice();
		Advisor advisor = new DefaultPointcutAdvisor(composablePointcut, advice);
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.addAdvice(advice);
		proxyFactory.setTarget(simpleBean);
		SimpleBean proxy = (SimpleBean) proxyFactory.getProxy();
		return proxy;
	}

	public static void main(String[] args) {
		SimpleBean simpleBean = new SimpleBean();

		ComposablePointcut composablePointcut = new ComposablePointcut(
				ClassFilter.TRUE, new GetterMethodMatcher());
		composablePointcut.union(new SetterMethodMatcher());
		SimpleBean proxy = getProxy(composablePointcut, simpleBean);
		test(proxy);
	}
}

TestAdvice.java

class TestAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] objects, Object object)
			throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("Before " + method);
	}

}

GetterMethodMatcher .java

class GetterMethodMatcher extends StaticMethodMatcher {

	@Override
	public boolean matches(Method method, Class claz) {
		// TODO Auto-generated method stub
		boolean result = method.getName().startsWith("get");
		return false;
	}

}

SetterMethodMatcher .java

class SetterMethodMatcher extends StaticMethodMatcher {

	@Override
	public boolean matches(Method method, Class claz) {
		// TODO Auto-generated method stub
		boolean result = method.getName().startsWith("get");
		return result;
	}

}

SimpleBean .java

class SimpleBean {
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

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


Before public int roseindia.net.union.SimpleBean.getAge()

Before public void roseindia.net.union.SimpleBean.setName(java.lang.String)

Before public java.lang.String roseindia.net.union.SimpleBean.getName()

Download this example code

Advertisements

Posted on: September 6, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.