1 控制流切入点
匹配一个类的某一个方法或该方法下的所有方法调用。
。实现代码如下:
package cuigh.spring.aop.step02;
import org.aopalliance.aop.Advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.Advisor;
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 ControlFlowPointcutExample {
static Log log = LogFactory.getLog(ControlFlowPointcutExample.class);
public static void main(String[] args) {
//创建目标
TestControlFlowPointcut target = new TestControlFlowPointcut();
//创建 通知
Advice advice = new MyBeforeAdvice();
//创建切入点 MyDynamicPointcut 方法 testNotAdvice() 被拦截
Pointcut pointcut = new ControlFlowPointcut(TestControlFlowPointcut.class,"testNotAdvice");
//创建通知者
Advisor advisor = new DefaultPointcutAdvisor(pointcut,advice);
//创建代理
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvisor(advisor);
TestControlFlowPointcut proxy = (TestControlFlowPointcut)pf.getProxy();
log.info("-----下面未被拦截------");
target.test(proxy);
log.info("-----下面被拦截------");
target.testNotAdvice(proxy);
}
}
package cuigh.spring.aop.step02;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/*
* 功能:控制流切入点示例类
* */
public class TestControlFlowPointcut {
static Log log = LogFactory.getLog(TestControlFlowPointcut.class);
public void test1(){
log.info(this.getClass().getName()+".test1()");
}
public void test2(){
log.info(this.getClass().getName()+".test2()");
}
public void test(TestControlFlowPointcut bean){
bean.test1();
bean.test2();
}
//对此方法进行拦截
public void testNotAdvice(TestControlFlowPointcut bean){
bean.test1();
bean.test2();
}
}
package cuigh.spring.aop.step02;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforeAdvice implements MethodBeforeAdvice {
static Log log = LogFactory.getLog(MyBeforeAdvice.class);
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
log.info("方法执行前拦截,被拦截方法="+method.getName());
}
}
程序输出:
2013-07-20 19:35:44,343 [ControlFlowPointcutExample.java 39] INFO - -----下面未被拦截------
2013-07-20 19:35:44,359 [TestControlFlowPointcut.java 10] INFO - cuigh.spring.aop.step02.TestControlFlowPointcut.test1()
2013-07-20 19:35:44,359 [TestControlFlowPointcut.java 14] INFO - cuigh.spring.aop.step02.TestControlFlowPointcut.test2()
2013-07-20 19:35:44,359 [ControlFlowPointcutExample.java 41] INFO - -----下面被拦截------
2013-07-20 19:35:44,359 [MyBeforeAdvice.java 15] INFO - 方法执行前拦截,被拦截方法=test1
2013-07-20 19:35:44,359 [TestControlFlowPointcut.java 10] INFO - cuigh.spring.aop.step02.TestControlFlowPointcut.test1()
2013-07-20 19:35:44,359 [MyBeforeAdvice.java 15] INFO - 方法执行前拦截,被拦截方法=test2
2013-07-20 19:35:44,359 [TestControlFlowPointcut.java 14] INFO - cuigh.spring.aop.step02.TestControlFlowPointcut.test2()