zoukankan      html  css  js  c++  java
  • spring学习 十二 AspectJ-based的通知入门 带参数的通知

    第一步:编写通知类

    package com.airplan.pojo;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class Advice {
        
            public void mybefore(String name1,int age1){
                System.out.println("前置"+name1 );
            }
            
            public void mybefore1(String name1){
                System.out.println("前置:"+name1);
            }
            
            public void myaftering(){
                System.out.println("后置 2");
            }
            
            public void myafter(){
                System.out.println("后置 1");
            }
            
            public void mythrow(){
                System.out.println("异常");
            }
            
            public Object myarround(ProceedingJoinPoint p) throws Throwable{
            
                System.out.println("执行环绕");
                System.out.println("环绕-前置");
                Object result = p.proceed();
                System.out.println("环绕后置");
                return result;
            }
    }

    第二步配置

    2 配置 spring 配置文件

      2.1 <aop:after/> 后置通知,是否出现异常都执行

      2.2 <aop:after-returing/> 后置通知,只有当切点正确执行时执行

      2.3 <aop:after/> 和 <aop:after-returing/> 和<aop:after-throwing/>执行顺序和配置顺序有关

      2.4 execution() 括号不能扩上 args
      2.5 中间使用 and 不能使用&& 由 spring 把 and 解析成&&
      2.6 args(名称) 名称自定义的.顺序和 demo1(参数,参数)对应
      2.7 <aop:before/> arg-names=” 名 称 ” 名 称 来 源 于expression=”” 中 args(),名称必须一样

        2.7.1 args() 有几个参数,arg-names 里面必须有几个参数
        2.7.2 arg-names=”” 里面名称必须和通知方法参数名对应

    <aop:config>
            <aop:aspect ref="myadvice">
                <!-- 
                    args(name1,age1):args中的参数必须与<aop:before />标签中arg-names的属性值一致
                 -->
                <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String,int)) and args(name1,age1)" id="mypoint"/>
                
              
                
                <aop:before method="mybefore" pointcut-ref="mypoint" arg-names="name1,age1"/>
                
                
                
            </aop:aspect>
        </aop:config>

    上面标蓝的部分一定要一致,并且要与通知方法的参数对应,连名称也要一样,虽然切点的参数名称不要求和args中的名称对应,但是数量要对应

  • 相关阅读:
    node.js结合wechaty实现微信机器人[基础篇]
    .env文件为NodeJS全局环境变量
    基于jquery实现一个提示插件
    Puppeteer实现一个超简单的自动化机器人
    Vue高仿阿里动态banner,制作组件
    css不常用属性
    Vue表单校验失败滚动到错误位置
    C# Func委托
    深入解析C# 4th 笔记(第一章)
    C# 笔记 XML基础
  • 原文地址:https://www.cnblogs.com/cplinux/p/9741750.html
Copyright © 2011-2022 走看看