1. 包围通知 性能测试
org.aopalliance.intercept.MethodInterceptor
综合了前置通知和后置通知, 除了一个重要区别, 我们可以修改方法的返回值, 还可以阻止目标方法的执行,可以将目标方法的实现换成新的代码
public class MessageWriter {
public void showMessage(){
System.out.println("this is a test");
}
}
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;
public class AroundInterceptor implements MethodInterceptor{
public Object invoke(MethodInvocation invocation) throws Throwable{
StopWatch sw=new StopWatch();
sw.start(invocation.getMethod().getName());
Object retVal=invocation.proceed();
sw.stop();
dumpInfo(invocation,sw.getTotalTimeMillis());
return retVal;
}
private void dumpInfo(MethodInvocation invocation,long ms){
java.lang.reflect.Method m=invocation.getMethod();
Object target=invocation.getThis();
Object[] args=invocation.getArguments();
System.out.println("Execute method: "+m.getName());
System.out.println("On Object of type: "+target.getClass().getName());
System.out.println("with arguments: ");
for(int x=0;x<args.length;x++){
System.out.println(" >"+args[x]);
System.out.print("\n");
}
System.out.println("took :"+ms + " ms");
}
}
import org.springframework.aop.framework.ProxyFactory;
public class test {
public static void main(String[] args) {
MessageWriter target =new MessageWriter();
ProxyFactory pf=new ProxyFactory(target);
pf.addAdvice(new AroundInterceptor());
MessageWriter proxy=(MessageWriter)pf.getProxy();
proxy.showMessage();
}
}
结果
this is a test
Execute method: showMessage
On Object of type: MessageWriter
with arguments:
took :0 ms
分享到:
相关推荐
aop学习
spring中AOP中标签加载通知,spring中AOP中标签加载通知spring中AOP中标签加载通知spring中AOP中标签加载通知,spring中AOP中标签加载通知
Spring IOC AOP学习示例代码,包含Spring常用操作示例和所有所需jar文件。参考博客:http://blog.csdn.net/daijin888888/article/details/51735291
Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结
spring的Aop中的前置通知,后置通知以及环绕通知简单代码
在AOP注解中一共分为五中类型的通知注解,接下来,我们通过实例,对这五种AOP注解进行讲解
Spring 使用AspectJ 实现 AOP之前置通知小例子,实际跑过,验证可信。
NULL 博文链接:https://microjava.iteye.com/blog/525796
Xml文件配置实现AOP通知
SSH+AOP学习资料源码 SSH+AOP学习资料源码 SSH+AOP学习资料源码 SSH+AOP学习资料源码 SSH+AOP学习资料源码
供学习AOP的人参考,DEMO在VS 2008下调试通过。
Spring Aop 学习笔记
关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用
SpringAop xml方式配置通知
SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
简单AOP操作面板学习的介绍
aop学习文档
AOP学习主要过程代码.pdf
NULL 博文链接:https://ihuning.iteye.com/blog/2227042