Spring 环绕通知
来源:互联网 发布:java file 新建文件夹 编辑:程序博客网 时间:2024/04/27 21:55
Spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别:1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知是不能决定的,他们只是在方法的调用前后执行通知而已,即目标方法肯定是要执行的。2)环绕通知可以控制返回对象,即你可以返回一个与目标对象完全不同的返回值,虽然这很危险,但是你却可以办到。而后置方法是无法办到的,因为他是在目标方法返回值后调用。
Spring 提供了Interceptor 接口来实现环绕通知。它只有一个invoke 方法,该方法接只接受MethodInvocation 参数。MethodInvocation 可以获得目标方法的参数,并可以通过proceed 方法调用原来的方法。代码如下:
1)环绕通知
Java代码
- public class Around implements MethodInterceptor
- {
- public Object invoke( MethodInvocation invocation)throws Throwable {
- System.out.println("Round.invoke()");
- System.out.println("Arguments:");
- Object[] args = invocation.getArguments();
- for(Object arg: args) {
- System.out.println(arg.getClass().getName() + ": " + arg);
- }
- System.out.println("Method name:" + invocation.getMethod().getName());
- //修改了目标方法返回值
- return invocation.proceed() + " in Round.invoke()";
- }
- }
2)目标对象
Java代码
- public class Target implements Advice
- {
- public String test(int i, String s, float f) {
- System.out.println("Target.test()");
- System.out.println("target: " + this);
- StringBuffer buf = new StringBuffer();
- buf.append( "i = " + i);
- buf.append( ", s = \"" + s + "\"");
- buf.append( ", f = " + f);
- return buf.toString();
- }
- }
3)接口定义
Java代码
- public interface Advice
- {
- String test(int i, String s, float f);
- }
4)配置文件
Xml代码
- <beans>
- <bean id="around" class="spring.Around"/>
- <bean id="aop" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces" value="spring.Advice" />
- <property name="interceptorNames">
- <list>
- <value>around</value>
- </list>
- </property>
- <property name="target">
- <bean class="spring.Target" />
- </property>
- </bean>
- </beans>
- Spring 环绕通知
- spring 的环绕通知
- Spring--AOP-环绕通知
- spring aop 环绕通知
- Spring AOP 环绕通知
- Spring-AOP-通知-环绕通知
- spring的前置通知,后置通知,环绕通知
- Spring AOP--返回通知,异常通知和环绕通知
- Spring AOP--返回通知,异常通知和环绕通知(顶)
- Spring返回通知、异常通知和环绕通知
- spring aop 环绕通知 基于@aspectJ
- spring aop 5 环绕通知around
- spring(十):aop环绕通知
- Spring AOP环绕通知小例子
- 环绕通知
- 环绕通知
- spring aop 环绕通知around和其他通知的区别
- spring aop 6 环绕通知记录应用的日志
- Hough变换检测直线
- sitemesh
- 读书笔记系列之一----《构建高性能WEB站点》
- Protocol Buffers使用入门
- dom4j解析XML字符串实例
- Spring 环绕通知
- Converting OVA for use with KVM / QCOW2
- androidのEditTex详细使用
- 排序题目一网打尽
- GetMemory错误讲解(指针练习)
- SQL语句
- 加速Web开发的9款知名HTML5框架
- 虚拟化系列-VMware vSphere 5.1 简介与安装
- SQL常用函数