Spring框架AOP的使用及个人对底层原理的理解

Aop使用步骤

配置aop信息

<aop:config> 相当于aop的根节点

配置切入点

<aop:piontcut> 切入点 可以理解为需要增强的方法的位置

如:

execution(* com.mmr.entity.People.*(..) )

表示这个切入点的位置在com.mmr.entity.People 这个类里;

第一个星号:表示访问权限是任意,也可以设置为private,protected。。。。。

第二个星号:表示People类下面的所有方法

(..)表示任意参数

包后面两个点表示包含子包下面的类,单点不包含子包

 

配置切入面

<aop:aspect>

ref 指定的是用什么类来增强,指向的是一个bean

配置切入时间

<aop:after>......此类标签指定切入时间

after---最终 相当于try catch 中的finally

after-returning 后置,方法无异常执行完后

method 指定用哪个方法来增强(ref中指向的类中的方法)

pointcut-ref 指定增强的切入点

 

配置示例:

<bean name="people" class="com.mmr.entity.People"></bean>

<bean name = "add" class = "com.mmr.entity.AddMethod"/>

<aop:config>

<aop:pointcut expression="execution(* com.mmr.entity.People.*(..) )" id="peoplecut"/>

<aop:aspect ref="add">

<aop:after method="after" pointcut-ref="peoplecut"/>

<aop:before method="before" pointcut-ref="peoplecut"/>

<aop:around method="around" pointcut-ref="peoplecut"/>

<aop:after-throwing method="throwing" pointcut-ref="peoplecut"/>

<aop:after-returning method="after_return" pointcut-ref="peoplecut"/>

</aop:aspect>

</aop:config>

 

注意:

spring Aop 底层采用了动态代理(jdk自带的动态代理 + cglib 动态代理)的方式来实现增强

所以如果被增强的类(委托类)实现了接口,则会采用jdk自带的动态代理方式,所以需要使用接口来接收,不能直接使用实现类接收!

而如果委托类未实现接口,则采用cglib动态代理的方式,所以可以用委托类直接接收

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值