spring框架的四种通知类型 前置通知 后置通知 异常通知 环绕通知 编码实现及测试

spring框架的五种通知类型 如下

通知类型 说明 接口
前置通知 在调用目标对象方法之前对请求进行权限检查 MethodBeforeAdvice
后置通知 在目标方法调用之后执行,一旦目标方法产生异常不会执行 AfterReturningAdvice
异常通知 当目标对象方法抛出异常时异常通知 ThrowsAdvice
环绕通知 在目标方法执行之前和执行之后都会执行,可以写一些非核心的业务逻辑,一般用来替代前置通知和后置通知 MethodInterceptor

前提:实现spring环境需要搭建好
spring框架的四种通知类型 前置通知 后置通知 异常通知 环绕通知 编码实现及测试_第1张图片

前置通知代码实现

编写前置通知访问控制拦截器AccessInterceptor 实现MethodBeforeAdvice接口,代码如下:

package interceptor;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class AccessInterceptor implements MethodBeforeAdvice{
	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		System.out.println("前置通知。。。。。。。。。。。。。。。。。。。");
	}
}

后置通知代码实现

编写后置通知的的拦截器,实现AfterReturningAdvice接口,代码如下:

package interceptor;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class GetIpInterceptor implements AfterReturningAdvice {
	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
		System.out.println("后置通知");
	}
}

异常通知代码实现

编写异常通知拦截器类,实现ThrowsAdvice接口

package interceptor;

import org.springframework.aop.ThrowsAdvice;

public class ExceptionInterceptor implements ThrowsAdvice {
	
	public void afterThrowing(Exception ex)
	{
		System.out.println("异常通知:"+ex.getMessage());
	}
}

环绕通知代码实现

编写环绕通知拦截器,需求是调用目标对象前后都要输出日志,实现MethodInterceptor接口
注意:环绕通知需要导AOP联盟(aopallianc)的包

package interceptor;

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

public class LogInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("调用方法之前环绕通知执行日志输出。。。。");
		invocation.proceed();
		System.out.println("调用方法之后环绕通知执行日志输出。。。。");
		return null;
	}
}

编写目标对象的接口 ProductServiceIfac

package service;

public interface ProductService {
	public void addProduct();
	
	public void updateProduct();
}

编写目标对象ProductServiceImpl,并且实现刚刚写的接口ProductServiceIfac

package service;

public class ProductServiceImpl implements ProductServiceIfac {

	@Override
	public void addProduct() {
		System.out.println("执行目标对象的addProduct()方法------------------");
	}

	@Override
	public void updateProduct() {
		System.out.println("执行目标对象的updateProduct()方法------------------");
	}
}

配置beans.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 
 	<!-- 配置目标对象 -->
 	<bean id="productService" class="service.ProductServiceImpl"></bean>
 	
 	<!-- 配置拦截器 -->
 	<bean id="exceptionInterceptor" class="interceptor.ExceptionInterceptor"></bean>
 	<bean id="accessInterceptor" class="interceptor.AccessInterceptor"></bean>
 	<bean id="logInterceptor" class="interceptor.LogInterceptor"></bean>
 	<bean id="getIpInterceptor" class="interceptor.GetIpInterceptor"></bean>
 	<!-- 配置代理工厂 -->
 	<bean id="productServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
 		<property name="target" ref="productService"></property>
 		<property name="interceptorNames"><!-- 拦截器名字,是一个数组 -->
 			<list>
 				<value>exceptionInterceptor</value>
 				<value>accessInterceptor</value>
 				<value>logInterceptor</value>
 				<value>getIpInterceptor</value>
 			</list>
 		</property>
 		<property name="proxyInterfaces" value="service.ProductServiceIfac"></property>
 	</bean>
 	
 </beans>

编写一个测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.ProductServiceIfac;

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");
		
		ProductServiceIfac serviceProxy=(ProductServiceIfac) act.getBean("productServiceProxy");		
		serviceProxy.addProduct(); 
	}
}

代码运行结果如下:
spring框架的四种通知类型 前置通知 后置通知 异常通知 环绕通知 编码实现及测试_第2张图片

你可能感兴趣的:(spring)