spring学习系列 --- AOP学习

1、AOP是什么? 面向切片编程

可以实现各种注释功能,在某个函数调用之前、运行以及调用之后,实现某些功能,并且可以访问该函数中的参数,以及打印出结果来。

使用的jar包主要是三个: AspectJ 

aopalliance.jar   

aspectjweaver-1.6.6.jar   

spring-aspects-4.0.6.RELEASE.jar

 

在bean.xml文件中,要申明一下  xmlns:aop="http://www.springframework.org/schema/aop"

后面在使用 AOP的时候,还要进行各种定义

2、AOP实现各种通知

前置通知、后置通知、环绕通知、返回通知以及异常通知

3、AOP config 实现

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="studentservice1"  class="com.studentService">
    </bean>
    <bean id="studentserviceaspect1" class="com.studentServiceAspect" >

    </bean>

    <aop:config>
        <aop:aspect id="dobefore" ref="studentserviceaspect1" >
            <aop:pointcut  expression="execution(* com.studentService.*(..))"  id="pc1"/>
            <aop:before method="doBefore" pointcut-ref="pc1" />
            <aop:after method="doAfter" pointcut-ref="pc1" />
        </aop:aspect>
    </aop:config>


</beans>

aop:pointcut 切面,就是 通过 execution 这个来匹配要进行处理的函数, execution(* com.studentService.*(..)) 前面第一个表示的是 返回值, 后面是去匹配方法,而括号里面的 .. , 表示的是函数传人的参数数目可以是任意的。

环绕通知,可以在方法调用前调用,也可以在方法调用后调用,以及可以获取方法返回值

异常通知,可以捕获运行过程中的发生的异常信息

4、在AOP相应的函数里面,去写各种通知,那么使用 

import org.aspectj.lang.JoinPoint;

JoinPoint 类,来捕获调用函数中的异常,参数,类别名,方法名等等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值