zoukankan      html  css  js  c++  java
  • spring aop编程

    1、AOP,面向切面编程(aspect Oriental programing),使用aop,可以将处理切面aspect的代码注入到主程序,通常主程序的主要目的不是处理这些切面aspect,可以防止代码混乱。拦截机 interceptor是AOP的另一中叫法。(其中使用的模式为代理模式,动态代理模式)。

    2、通知advise:在aspect的某个连接点(在Spring的aop中,一个连接点就是一个方法的执行)上执行的动作。

    切入点pointcut:匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行。

    目标对象:target object,被一个活多个切面所通知的对象。也称被通知对象。

    3、spring中的aop

    加入支持aop的jar包:

    aopalliance.jar

    aspectjrt.jar

    aspectjweaver.jar

    spring-aop.jar

    spring-aspect.jar

    (1)在xml中加入aop的命名空间:

    <?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">
    
        
    </beans>

    (2)写aspect类和切面的通知

    <bean id="myAspect" class="com.spring1.util.Aspect"/>
        <aop:config>
            <!--切面 -->
            <aop:aspect ref="myAspect">
                <!-- 切入点 执行(方法访问修饰符? 方法返回类型   声明类? 方法名(方法参数类型) 抛出异常?) -->
                <aop:pointcut expression="execution(* com.spring1.dao..*.*(..))" id="point1"/>
                <aop:before method="beforeAdvise" pointcut-ref="point1"/>
                <aop:after method="afterAdvise" pointcut-ref="point1"/>
                <!-- 返回通知,最后的参数是通知参数的名 -->
                <aop:after-returning method="afterReturning" pointcut-ref="point1" returning="ret"/>
                <!-- 异常通知,最后参数是异常的参数的名 -->
                <aop:after-throwing method="afterThrowing" pointcut-ref="point1" throwing="e"/>
            </aop:aspect>
        </aop:config>

    (3)将aspect类交给Spring管理,配置aspect类的bean

    上边已配置过

    (4)根据aop中的通知写通知函数:

    //切面类
    public class Aspect {
        //通知...
        void beforeAdvise(){
            System.out.println("before");
        }
        
        void afterAdvise(){
            System.out.println("after");
        }
        
        void afterReturning(Object ret){
            System.out.println("afterReturning " +ret.toString());
        }
        
        void afterThrowing(Exception e){
            System.out.println("afterThrowing " + e.getMessage());
        }
    }

    (5)当上边的都需要些的时候可以直接用 环绕通知代替:

    <bean id="myAspect" class="com.spring1.util.Aspect"/>
        <aop:config>
            <!--切面 -->
            <aop:aspect ref="myAspect">
                <!-- 切入点 执行(方法访问修饰符? 方法返回类型   声明类? 方法名(方法参数类型) 抛出异常?) -->
                <aop:pointcut expression="execution(* com.spring1.dao..*.*(..))" id="point1"/>
                <aop:around pointcut-ref="point1" method="aroundAdvise"/>
            </aop:aspect>
        </aop:config>
        void aroundAdvise(ProceedingJoinPoint pjp){
            try {
                System.out.println("前置");
                Object object = pjp.proceed(); 
                System.out.println(object.toString());
            } catch (Throwable e) {
                e.printStackTrace();
                System.out.println("异常");
            }finally{
                System.out.println("最终");
            }
        }

    后置通知就是返回后的通知

    最终通知又叫finally通知。

  • 相关阅读:
    ArcGIS Engine常用网址
    【转载】C# 跨线程调用控件
    win7电脑更新显卡不兼容,造成电脑开机黑屏
    js 字符串时间比较大小
    jquery 实现 table搜索功能
    php抓取网页数据遇到的问题
    php根据修改时间删除指定目录下文件
    ubuntu18.10安装redis遇到问题
    DEV通过FindFilterText自动检索gridview内容
    dev gridview 设置分组
  • 原文地址:https://www.cnblogs.com/hpustudent/p/4314732.html
Copyright © 2011-2022 走看看