Spring学习笔记-第二章-Spring AOP

5 篇文章 0 订阅
1 篇文章 0 订阅

目标类 + 额外功能 = 代理类
额外功能叫 增强
目标类叫 切入点 或 切面
将增强 加入到 切入点, 叫编织
AOP是面向切面编程的一种实现
面向切面编程(AOP)完善spring的依赖注入(DI),面向切面编程在spring中主要表现为两个方面
1.面向切面编程提供声明式事务管理
2.spring支持用户自定义的切面

面向切面编程(aop)是对面向对象编程(oop)的补充,
面向对象编程将程序分解成各个层次的对象,面向切面编程将程序运行过程分解成各个切面。
AOP从程序运行角度考虑程序的结构,提取业务处理过程的切面,oop是静态的抽象,aop是动态的抽象,
是对应用执行过程中的步骤进行抽象,,从而获得步骤之间的逻辑划分。

aop框架具有的两个特征:
1.各个步骤之间的良好隔离性
2.源代码无关性

举个例子:一台主机(其中CPU带有核显)在不装独立显卡的时候也可以正常工作,但是装备独立显卡会在画面渲染方面更出色,色彩更好。

前值增强

public class MyBeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		System.out.println("this is my before adcive");
		//获取目标方法的名称
		String method = arg0.getName();
		System.out.println("the method"+method);
		//获取目标方法参数的实参列表
		for (int i=0;i<arg1.length;i++) {
			System.out.println("the arg1:"+arg1[i]);
		}
		//获取目标实现类的类路径
		System.out.println("the arg2"+arg2.getClass());
		System.out.println("the is MyBeforeAdvice");
	}

}

后置增强

public class MyAfterAdvice implements AfterReturningAdvice {

	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
		System.out.println("this is my after advice");
		//获取反参参数的类型
		System.out.println("the arg0"+arg0.getClass());
		//获取目标方法名称
		System.out.println("the arg1"+arg1.getName());
		//获取目标方法参数的实参列表
		for (int i = 0; i < arg2.length; i++) {
			System.out.println("the arg2"+arg2[i]);
		}
		//获取目标实现类的类路径
		System.out.println("the arg3"+arg3.getClass());
		System.out.println("the is afterReturning advice");
		
	}

}

异常增强

public class MyThrowAdvice implements ThrowsAdvice {
	public void afterThrowing(Method method,Object[] args,Object target,Exception ex) {
		System.out.println("this is my throw advice");
		System.out.println(target+"里面的:"+method.getName()+"产生了异常:"+ex.getMessage());
	}
}

环绕增强
由于启动了环绕增强,目标类的方法(原核心代码)将不会被执行,而被proceed取代 并返回目标类的反参

public class MyInterceptor implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation arg0) throws Throwable {
		System.out.println("this is my interceptor advice");
		//获取目标实现类对象
		System.out.println("arg0.getThis()"+arg0.getThis().getClass().toString());
		//获取目标方法名称
		System.out.println("arg0.getMethod();"+arg0.getMethod().getName());
		//获取目标方法参数的实参列表
		Object[] arguments = arg0.getArguments();
		for (int i = 0; i < arguments.length; i++) {
			System.out.println("arg0.getArguments()"+arguments[i]);
		}
		//由于启动了环绕增强,目标类的方法将不会被执行,而被proceed取代 并返回目标类的反参
		//执行目标方法
		Object object = arg0.proceed();
		return object;
	}

}

增强功能

<!-- 增强功能 -->
	<bean id="myBeforeAdvice" class="com.gxy.aop.MyBeforeAdvice"></bean>
	<bean id="myAfterAdvice" class="com.gxy.aop.MyAfterAdvice"></bean>
	<bean id="myThrowAdvice" class="com.gxy.aop.MyThrowAdvice"></bean>
	<bean id="myInterceptor" class="com.gxy.aop.MyInterceptor"></bean>

编织

<!-- AOP配置  编织 -->
	<aop:config>
		<!-- 指定切入点 -->
		<aop:pointcut expression="execution(* com.gxy.service.*.*(..))" id="myPointCut"/>
		<!-- 编织 -->
		<aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="myPointCut"/>
		<aop:advisor advice-ref="myAfterAdvice" pointcut-ref="myPointCut"/>
		<aop:advisor advice-ref="myThrowAdvice" pointcut-ref="myPointCut"/>
		<aop:advisor advice-ref="myInterceptor" pointcut-ref="myPointCut"/>
	</aop:config>

** * com.gxy.service. * . * (…) **
↑ ↑ ↑ ↑ ↑
1 2 3 4 5
1: 通配接口或者普通方法
2: 包名
3: 类名
4: 方法名
5: 参数

AOP注解

  1. 写一个软接口 创建或自定义一个注解
public @interface AnnotationLog{
	//软接口
}
  1. 增强类照常写
  2. 修改切入点
<aop:config>
		<aop:pointcut expression="@annotation(com.gxy.aop.AnnotationLog)" id="myPointCut"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值