zoukankan      html  css  js  c++  java
  • Spring、AOP详解

    如何配置AOP查看:Spring、Hello AOP

    1.对于拦截规则@Pointcut的介绍:

    	@Pointcut("execution (* cn.raffaello.service..*.*(..))")
    	private void anyMethod(){} // 声名一个切入点,声名方式比较奇特

    注解:

    // *      返回值类型  *为所有类型的返回值
    // cn.raffaello.service  所拦截的包
    // ..      如果使用..则表示要对拦截的包及下的所有子包进行拦截
    // *.*   类.方法名    所拦截的包下的类.方法
    // (..)   所拦截方法的参数 ..为不限定参数及类型  例(String,Integer,String)


    2.前置通知、后置通知、最终通知、环绕通知、例外通知在Spring处理中存在的位置:


    	@Before("anyMethod()") 
    	public void doAccessCheck(){
    		System.out.println("前置通知");
    	}
    	@AfterReturning("anyMethod()")
    	public void doAfterReturning(){
    		System.out.println("后置通知");
    	}
    	@After("anyMethod()")
    	public void doAfter(){
    		System.out.println("最终通知");
    	}
    	@Around("anyMethod()")
    	public void doAround(ProceedingJoinPoint pjp) throws Throwable{
    		System.out.println("环绕通知前");
    		pjp.proceed(); // proceed()  执行业务方法,可以在此前后添加拦截判断,权限状态等
    		System.out.println("环绕通知后");
    	}
    	@AfterThrowing("anyMethod()")
    	public void doAfterThrowing(){
    		System.out.println("例外通知"); // 异常通知
    	}
    	


    3.拦截特定参数、返回值、异常的方法

    	// 拦截只有一个参数,并且参数类型为String
    	@Before("anyMethod() && args(name)") 
    	public void doAccessCheck(String name){
    		System.out.println("前置通知  Name:"+name);
    	}

    	// 拦截返回值类型是String的方法
    	@AfterReturning(pointcut="anyMethod()",returning="returnValue")
    	public void doAfterReturning(String returnValue){
    		System.out.println("后置通知   return:"+returnValue);
    	}

    	// 拦截异常类型为Exception的异常
    	@AfterThrowing(pointcut="anyMethod()",throwing="ex")
    	public void doAfterThrowing(Exception ex){
    		System.out.println("例外通知 例外:"+ex); // 异常通知
    	}


  • 相关阅读:
    $python打包工具pyinstaller的用法
    $命令行参数解析模块argparse的用法
    $用ConfigParser模块读写conf配置文件
    $用python-docx模块读写word文档
    $git学习总结系列(4)——gitignore文件
    $ git学习总结系列(3)——分支管理
    $ 专治各种python字符编码问题疑难杂症
    $python数据分析基础——初识matplotlib库
    $ python-json模块的基本用法
    $python虚拟化运行环境——virtualenv
  • 原文地址:https://www.cnblogs.com/raphael5200/p/5114736.html
Copyright © 2011-2022 走看看