Home Tutorial Spring Spring3 Aop Control Flow Pointcut

 
 

Share on Google+Share on Google+
Control Flow Pointcut
Posted on: September 6, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn about Spring AOP CotrolFlowPointcut.

Control Flow Pointcut Example

Spring AOP Control Flow Pointcut is Similar to AspectJ pointcut, but less powerful. It matches the current call stack. It can be implemented using org.springframework.aop.support.ControlFlowPointcut

SimpleClass.java

package roseindia.net.coltrolFlowpointcut;

public class SimpleClass {
	public void sayHi() {
		System.out.println("Hello Friend");
	}
}

TestAdvice.java

package roseindia.net.coltrolFlowpointcut;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.MethodBeforeAdvice;

public class TestAdvice implements MethodBeforeAdvice {

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

}

TestControlFlow.java

package roseindia.net.coltrolFlowpointcut;

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.ControlFlowPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class TestControlFlow {
	public void test() {
		SimpleClass target = new SimpleClass();
		Pointcut pointcut = new ControlFlowPointcut(TestControlFlow.class,
				"controlFlowTest");
		Advice advice = new TestAdvice();
		ProxyFactory proxyFactory = new ProxyFactory();

		Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
		proxyFactory.addAdvisor(advisor);
		proxyFactory.setTarget(target);
		SimpleClass simpleProxy = (SimpleClass) proxyFactory.getProxy();
		System.out.println("Calling Normally");
		simpleProxy.sayHi();
		System.out.println("Calling in ControlFlow");
		controlFlowTest(simpleProxy);
	}

	public void controlFlowTest(SimpleClass simpleClass) {
		simpleClass.sayHi();
	}

}

MainClaz.java

package roseindia.net.coltrolFlowpointcut;

public class MainClaz {
	public static void main(String[] args) {
		TestControlFlow testControlFlow = new TestControlFlow();
		testControlFlow.test();
	}
}

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


Calling Normally
Hello Friend
Calling in ControlFlow
Calling before public void roseindia.net.coltrolFlowpointcut.SimpleClass.sayHi()
Hello Friend

Download this example code

Advertisement

Related Tags for Control Flow 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 6, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com