Spring在使用后置通知与环绕通知时遇到的问题

声明一个通知类 同时继承MethodBeforeAdvice、AfterReturningAdvice和MethodInterceptor

package com.zx.aop;


import java.lang.reflect.Method;

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

public class MyMethodAdvice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor {
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("前置通知");
	}

	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("后置通知");
	}
	 
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("调用方法前");
		Object proceed = invocation.proceed();
		System.out.println("调用方法后");
		return proceed;
	}

}

配置xml

	<!-- 1.配置被代理的对象 -->
	<bean id="test1Service" class="com.zx.aop.Test1Service">
		<property name="name" value='zx'></property>
	</bean>
	<!-- 2.配置通知对象 -->
	<bean id="myMethodAdvice" class="com.zx.aop.MyMethodAdvice">
	</bean>
	<!-- <bean id="MyMethodInterceptor" class="com.zx.aop.MyMethodInterceptor">
	</bean> -->
	<!-- 3.配置代理对象   -->
	<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
	<!-- 配置代理接口  代理对象将实现代理的接口 可以不设置-->
	<!-- <property name="proxyInterfaces">
		<list>
			<value>com.zx.aop.TestServiceTnter</value>
		</list>
	</property> -->
	<!-- 4.把通知织入代理对象 可以是list -->
	<property name="interceptorNames">
	<list>
	<value>myMethodAdvice</value>
<!-- 	<value>MyMethodInterceptor</value> -->
	</list>
	</property>
	<!-- 5.配置被代理对象 必须要有 -->
	<property name="target" ref="test1Service"></property>
	</bean>

运行代码

TestServiceTnter ts1=(TestServiceTnter) context.getBean("proxyFactoryBean");
		ts1.sayHello();
输出

调用方法前
前置通知
Hello zx
后置通知
调用方法后


将前置通知后置通知与环绕通知类分开实现

前置加后置类

package com.zx.aop;


import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class MyMethodAdvice implements MethodBeforeAdvice,AfterReturningAdvice {
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("前置通知");
	}

	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("后置通知");
	}
	
}

环绕通知类

package com.zx.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyMethodInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("调用方法前");
		Object proceed = invocation.proceed();
		System.out.println("调用方法后");
		return proceed;
	}

}

配置xml

<!-- 1.配置被代理的对象 -->
	<bean id="test1Service" class="com.zx.aop.Test1Service">
		<property name="name" value='zx'></property>
	</bean>
	<!-- 2.配置通知对象 -->
	<bean id="myMethodAdvice" class="com.zx.aop.MyMethodAdvice">
	</bean>
	<bean id="MyMethodInterceptor" class="com.zx.aop.MyMethodInterceptor">
	</bean>
	<!-- 3.配置代理对象   -->
	<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
	<!-- 配置代理接口  代理对象将实现代理的接口 可以不设置-->
	<!-- <property name="proxyInterfaces">
		<list>
			<value>com.zx.aop.TestServiceTnter</value>
		</list>
	</property> -->
	<!-- 4.把通知织入代理对象 可以是list -->
	<property name="interceptorNames">
	<list>
	<value>myMethodAdvice</value>
	<value>MyMethodInterceptor</value>
	</list>
	</property>
	<!-- 5.配置被代理对象 必须要有 -->
	<property name="target" ref="test1Service"></property>
	</bean>

运行代码不变 运行结果

前置通知
调用方法前
Hello zx
调用方法后
后置通知

更改xml中interceptorNames属性中两个通知类的顺序 再次运行 结果如下

调用方法前
前置通知
Hello zx
后置通知
调用方法后

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值