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 类,来捕获调用函数中的异常,参数,类别名,方法名等等。

猜你喜欢

转载自blog.csdn.net/hwang4_12/article/details/82951177
今日推荐