AOP__基于基于Schema-based方式实现

本文详细介绍了Spring中的Schema-based AOP实现方式,包括前置、后置、环绕和异常通知的创建与配置。每个通知都需要实现特定的接口,并在spring配置文件中进行声明。此外,还提及了通过代理类进行配置的方法,并提供了相关的测试说明。
摘要由CSDN通过智能技术生成

spring 提供了 2 种 AOP 实现方式:(1)Schema-based ,(2)AspectJ
Schema-based:每个通知都需要实现接口或类,配置 spring 配置文件时在aop:config配置

新建通知类:
前置通知,后置通知,环绕通知,异常通知(AspectJ有最终通知):
每个通知都要有自己类,因为它们所继承的接口不同,

前置通知继承org.springframework.aop.MethodBeforeAdvice;

后置通知继承org.springframework.aop.AfterReturningAdvice;

环绕通知继承org.aopalliance.intercept.MethodInterceptor;

异常通知继承org.springframework.aop.ThrowsAdvice;

arg0: 切点方法对象 Method 对象
arg1: 切点方法参数
arg2:切点在哪个对象中

前置:

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
/**
 *
 */
	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("这是前置通知");
	}

}

后置:

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodAfterAdvice implements AfterReturningAdvice{
	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("这是后置通知");
	}
}

环绕:

import
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

偷偷学习被我发现

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

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

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

打赏作者

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

抵扣说明:

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

余额充值