天天看点

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

后置通知

调用方法后