SpringAOP底层实现与配置(详细)

SpringAOP 底层实现方式

1:基于jdk的动态代理(需要源对象必须实现至少一个接口)

	通过Proxy.newProxyInstance(Classloader,Interfaces.class,InvocationHandler)
	new InvocationHandler(){
		public Object invoke(Object proxy, Method method(源对象的方法对象), Object[] args(该方法传递的参数))	
	}


	 ---------------------
	丨JDK的动态代理(接口)丨
	 ---------------------

TaetgetInterface proxyTarget =(TargetInterFace) Proxy.newInstance(ClassLoader,Class[] Interfaces.class, new InvocationHandler(){
	@Override	
	public Object invoke(Object proxy, Mehtod method, Object...args){
		//前置增强代码 可以选择增强参数等,也可以做不关对象方法本身的操作
		Object invoke = method.invoke(target,args);
		//后置增强代码,可以选择增强返回值等,也可以做其他操作
	}
});

proxyTarget.show();(以后使用的target对象调用的show方法 都是使用代理对象proxyTarget调用);


	 --------------------------------------
	丨  CGlib实现动态代理(无接口,非final) 丨
	 --------------------------------------

Enhancer enhancer = new Enhancer();
enhancer.setSuperClass(Target.class);
enhancer.setCallBack(new MethodInterceptor(){
	@Override
	public Object interceptor(Object proxy, Mehtod method, Object...args, MethodProxy methodProxy){
		//前置增强方法
		Object invoke = method.invoke(target,args);
		//后置增强方法
	}
});
Target proxyTarget = enhancer.create(); //CGlib的原理是使用继承的思想,所以内存中动态生成的代理对象是Target的子类对象
proxyTarget.show();		(以后使用的target对象调用的show方法 都是使用代理对象proxyTarget调用);	


	生成与源对象一样实现相同接口的类.当源对象调用某方法的时候,事实是生成的代理对象调用invoke方法.
	可以在invoke方法中使用反射调用源对象的该method方法 method.invoke(源对象,args);
	args可以做更改;该method方法执行后的返回值,也可以更改,称之为 参数增强和返回值增强


2:基于CGlib的动态代理(将源对象作为父类,运行期在内存中生成该源对象类的子类) 引入依赖aspectjweaver 缺点:无法实现final修饰的方法.私有属性修饰的方法不确定.

	通过Enhancer enhancer = new Enhancer(); 
	enhancer.setSuperClass(源对象类字节码对象); 
	enhancer.setCallBack(方法拦截器对象(接口) new MethodInterceptor(){
		public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy){}
	})此处的intercept方法类似于jdk动态设计模式的invoke方法.多的MethodProxy对象是该方法的代理执行对象,可以实现部分method对象的功能

SpringAOP 在Spring的配置文件中如何配置?
1:XML文档配置
引入依赖aspectjweaver 引入aop约束;

	定义切面类的bean
	<bean id="myAspect" class="com.itheima.MyAspect">

	<aop:config>
		<aop:aspect id="aspect" ref="myAspect">

			id代表给该pointcut设置的唯一标识,第一个 * 代表 返回值 (权限修饰可以省略) 方法参数里面的..代表任意参数类型和数量
			包后面的..代表该包下的直接或者间接的所有类

			<aop:pointcut id="point1" expression="execution(* *.*..*(..))">
	按执行先后顺序

			<aop:around method="切面类中的around方法" pointcut="point1">

				<aop:around method="切面类中的around方法" pointcut="execution(void com.itheima.Target.getStr(int, int))">

				<aop:around method="切面类中的around方法" pointcut="execution(void com.itheima.Target.getStr(..))">

				<aop:around method="切面类中的around方法" pointcut="execution(void com.itheima.Target.*(..))">

				<aop:around method="切面类中的around方法" pointcut="execution(void com.itheima.*.*(..))">

				## *** 最常用 *** ##	(itheima包下的所有直接类的所有方法.不管参数和返回值类型)
				<aop:around method="切面类中的around方法" pointcut="execution(* com.itheima.*.*(..))">
			
				iehtima包下的所有直接类或者间接类的所有方法,不论参数和返回值类型
				<aop:around method="切面类中的around方法" pointcut="execution(* com.itheima..*.*(..))">


			<aop:before method="切面类中的before方法" pointcut="point1">

			<aop:around method="切面类中的around方法" pointcut="point1">

			<aop:after method="切面类中的after方法" pointcut="point1">  在spring'中默认被finally修饰

			<aop:afterReturning method="切面类中的afterReturning方法" pointcut="point1"> 
			此处可以指定returning = "returnValue" 指定参数Object returnValue为源对象执行源方法后的返回值为returnValue.此时可以做增强返回值操作

			<aop:afterThrowing method="切面类中的afterThrowing方法" point="point1"> 尽在方法抛出未被捕获的异常的时候执行
			

		</aop:aspect>
	</aop:config>

SpringAOP 注解:

使用注解,一定要定义组件扫描:否则Spring不会去扫描定义类上的注解
<context:component-scan base-package="com.itheima"/>

而且需要设置aop自动代理 
<aop:aspectj-autoproxy/>

注解:
@EnableAspectJAutoProxy(aop的aspectj自动代理 == <aop:aspectj-autoproxy/>)

@Aspect("定义该类为一个切面类"),依赖于该类的组件注解@Component("myAspect")

@Around(value = "execution(* com.itheima.*.*(..))") 定义环绕增强方法

@Before(value = "execution(* com.itheima.*.*(..))") 定义前置增强方法.

@Around(value = "execution(* com.itheima.*.*(..))") 定义环绕增强方法

@After(value = "execution(* com.itheima.*.*(..))") 定义后置增强方法(finally修饰)

@AfterReturning(value = "execution(* com.itheima.*.*(..))") 定义后置增强方法
此处可以指定returning = "returnValue" 指定参数Object returnValue为源对象执行源方法后的返回值为returnValue.此时可以做增强返回值操作
@AfterReturning(returning="returnValue" value="execution(* com.itheima.*.*(..))")

@AfterThrowing(value = "execution(* com.itheima.*.*(..)") 定义出现未被捕获的异常的时候执行的方法

@Pointcut(value = "execution(* com.itheima.*.*(..))") 定义在任意方法上,该方法的全路径方法名可以作为该value的替代命名;
private void do(){}

以上的Around,Before,After等注解可以简写为:

@Around("do()") value = 是默认属性,本身就可以省略.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP底层实现原理主要是基于动态代理。Spring AOP通过使用代理模式,在运行时动态地为目标对象生成一个代理对象,然后通过代理对象来完成对目标对象的方法增强。 具体实现步骤如下: 1. 定义切面类:切面类是一个普通的Java类,用于定义切点和增强逻辑。切点是指确定在何处应用增强逻辑的表达式。增强逻辑是指在切点处插入的具体行为,如日志记录、事务管理等。 2. 配置切面:在Spring配置文件中,通过<aop:aspect>标签配置切面类,并使用<aop:pointcut>标签定义切点表达式。 3. 创建代理对象:在Spring容器启动时,会解析配置文件并扫描所有的Bean对象。当Spring发现某个Bean对象需要进行AOP增强时,会为该对象动态地创建一个代理对象。代理对象可以通过JDK动态代理或者CGLIB动态代理来创建。 4. 方法调用时的增强逻辑:当通过代理对象调用方法时,实际上是调用了代理对象中的方法。在代理对象的方法中,会根据切点表达式判断是否需要插入增强逻辑。如果需要插入增强逻辑,则会在方法的前后或者异常抛出时执行相应的增强操作。 总结起来,Spring AOP底层实现原理是通过动态代理,在运行时为目标对象生成代理对象,并在代理对象中实现对目标对象方法的增强。这个过程是在Spring容器启动时进行的,通过配置文件中的切面定义和切点表达式,确定增强逻辑的插入位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值