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

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

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

前提:实现spring环境需要搭建好
在这里插入图片描述

前置通知代码实现

编写前置通知访问控制拦截器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(); 
	}
}

代码运行结果如下:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值