Composable Pointcut-Intersection


 

Composable Pointcut-Intersection

In this tutorial you will learn how to use ComposablePointcut.Intersection to perform Intersection Operation.

In this tutorial you will learn how to use ComposablePointcut.Intersection to perform Intersection Operation.

ComposablePointcut-Intersection

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. In this tutorial you will see an example of Intersection Using Composable Pointcut.

MainClaz.java

package roseindia.net.intersection;

import java.lang.reflect.Method;

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 {
	public static void main(String[] args) {
		SimpleBean target = new SimpleBean();

		ComposablePointcut composablePointcut = new ComposablePointcut(
				ClassFilter.TRUE, new GetterMethodMatcher());
		composablePointcut.intersection(new GetAgeMethodMatcher());
		SimpleBean proxy = getProxy(composablePointcut, target);
		test(proxy);
	}

	private static SimpleBean getProxy(ComposablePointcut composablePointcut,
			SimpleBean target) {
		Advisor advisor = new DefaultPointcutAdvisor(composablePointcut,
				new TestAdvice());

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

		return (SimpleBean) proxyFactory.getProxy();
	}

	private static void test(SimpleBean simpleBean) {
		simpleBean.getAge();
		simpleBean.getName();
		simpleBean.setName("vinay");
	}
}

GetterMethodMatcher.java

class GetAgeMethodMatcher extends StaticMethodMatcher {
	public boolean matches(Method method, Class cls) {
		boolean result = "getAge".equals(method.getName());
		return result;
	}
}

GetAgeMethodMatcher.java

class GetAgeMethodMatcher extends StaticMethodMatcher {
	public boolean matches(Method method, Class cls) {
		boolean result = "getAge".equals(method.getName());
		return result;
	}
}

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

}

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.intersection.SimpleBean.getAge()

Download this example code

Ads