zoukankan      html  css  js  c++  java
  • 基于XML的开发

    基于XML的开发

    1.定义一个切面类

    /**
     * Created by zejian on 2017/2/20.*/
    public class MyAspectXML {
    
        public void before(){
            System.out.println("MyAspectXML====前置通知");
        }
    
        public void afterReturn(Object returnVal){
            System.out.println("后置通知-->返回值:"+returnVal);
        }
    
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("MyAspectXML=====环绕通知前");
            Object object= joinPoint.proceed();
            System.out.println("MyAspectXML=====环绕通知后");
            return object;
        }
    
        public void afterThrowing(Throwable throwable){
            System.out.println("MyAspectXML======异常通知:"+ throwable.getMessage());
        }
    
        public void after(){
            System.out.println("MyAspectXML=====最终通知..来了");
        }
    }
    View Code

    2.通过配置文件的方式声明如下:

    <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"
           xmlns:context="http://www.springframework.org/schema/context"
           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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--<context:component-scan base-package=""-->
    
        <!-- 定义目标对象 -->
        <bean name="productDao" class="com.zejian.spring.springAop.dao.daoimp.ProductDaoImpl" />
    
        <!-- 定义切面 -->
        <bean name="myAspectXML" class="com.zejian.spring.springAop.AspectJ.MyAspectXML" />
        <!-- 配置AOP 切面 -->
        <aop:config>
            <!-- 定义切点函数 -->
            <aop:pointcut id="pointcut" expression="execution(* com.zejian.spring.springAop.dao.ProductDao.add(..))" />
    
            <!-- 定义其他切点函数 -->
            <aop:pointcut id="delPointcut" expression="execution(* com.zejian.spring.springAop.dao.ProductDao.delete(..))" />
    
            <!-- 定义通知 order 定义优先级,值越小优先级越大-->
            <aop:aspect ref="myAspectXML" order="0">
                <!-- 定义通知
                method 指定通知方法名,必须与MyAspectXML中的相同
                pointcut 指定切点函数
                -->
                <aop:before method="before" pointcut-ref="pointcut" />
    
                <!-- 后置通知  returning="returnVal" 定义返回值 必须与类中声明的名称一样-->
                <aop:after-returning method="afterReturn" pointcut-ref="pointcut"  returning="returnVal" />
    
                <!-- 环绕通知 -->
                <aop:around method="around" pointcut-ref="pointcut"  />
    
                <!--异常通知 throwing="throwable" 指定异常通知错误信息变量,必须与类中声明的名称一样-->
                <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="throwable"/>
    
                <!--
                     method : 通知的方法(最终通知)
                     pointcut-ref : 通知应用到的切点方法
                    -->
                <aop:after method="after" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config>
    </beans>
    View Code
  • 相关阅读:
    for, 类型转换和使用方法
    笔记,随时更改
    控制流程之while循环, for循环
    赋值,逻辑,运算符, 控制流程之if 判断
    常量,基本数据类型,输入输出,基本运算符
    介绍python由来, 安装python3.8.3 及其变量的定义, 小整数池
    数组去重多个条件
    vue 自定义指令
    截取指定名字的url参数
    常用的js
  • 原文地址:https://www.cnblogs.com/yunhemeihe/p/12080229.html
Copyright © 2011-2022 走看看