Spring AOP 详解

Spring AOP 详解

出处:http://www.cnblogs.com/frankliiu-java/archive/2010/01/05/1639664.html

AOP中的概念 



Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象(包括切入点的描述和通知的描述)。 




Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法, 


因为spring只支持方法型的连接点,实际上joinpoint还可以是field或者构造器。 




Pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义。 



Advice(通知):所谓通知是指拦截到jointpoint之后所要做的事情就是通知。通知分为前置通知、后置通知、异常通知、最终通知、环绕通知。 


Target(目标对象):代理的目标对象 



Weave(织入): 指将aspects应用到target对象并导致proxy对象创建的过程称为织入 



Introducton(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field

Spring提供了两种切面使用方式,实际工作中我们可以选用其中一种 
1 基于xml配置方式进行AOP开发 
2 基于注解方式进行AOP开发  

(一)基于注解的方式 

下面是基于注解的方式 

Java代码 

  1. <?xml version=”1.0″ encoding=”UTF-8″?>  
  2. <beans xmlns=“http://www.springframework.org/schema/beans”  
  3.        xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  4.        xmlns:context=“http://www.springframework.org/schema/context”   
  5.        xmlns:aop=“http://www.springframework.org/schema/aop”        
  6.        xsi:schemaLocation=”http://www.springframework.org/schema/beans  
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”>  
  10.         <aop:aspectj-autoproxy/><!– 启动对@AspectJ注解的支持 –>  
  11. </beans>  
Java代码 

  1. import org.aspectj.lang.ProceedingJoinPoint;  
  2. import org.aspectj.lang.annotation.After;  
  3. import org.aspectj.lang.annotation.AfterReturning;  
  4. import org.aspectj.lang.annotation.AfterThrowing;  
  5. import org.aspectj.lang.annotation.Around;  
  6. import org.aspectj.lang.annotation.Aspect;  
  7. import org.aspectj.lang.annotation.Before;  
  8. import org.aspectj.lang.annotation.Pointcut;  
  9. import org.springframework.stereotype.Component;  
  10.   
  11. @Aspect @Component  
  12. public class MyInterceptor {  
  13.   
  14. /** 
  15.      *@Pointcut :表示规定切入点  
  16.      *execution() 语法规范 
  17.      * 第一个“*”表示任意返回结果类型 
  18.      * “cn.itcast.service.impl.PersonServiceBean”:表示对此类进行拦截, 
  19.      * 如果是cn.itcast.service..*.*:表示对包cn.itcast.service以及子包里所 
  20. 有的类的所有方法进行拦截, 
  21.      * (..)表示参数  
  22.      */   
  23.   
  24.       
  25.     @Pointcut(“execution(* com.mingbai.springaop.PersonServiceBean.*(..))”)  
  26.     private void anyMethod(){}//声明一个切入点  
  27.       
  28. /*  @Before(“anyMethod()”) 
  29.     public void doAccessCheck(){ 
  30.         System.out.println(“前置通知”); 
  31.     }*/  
  32.       
  33.     //此时的前置通知,只能拦截到参数个数和类型匹配的方法  
  34.     //args(name)中的name必须和方法doAccessCheck的参数一至  
  35.     @Before(“anyMethod() && args(name)”)  
  36.     public void doAccessCheck(String name){  
  37.         System.out.println(name+“前置通知”);  
  38.     }  
  39.       
  40. /*  @AfterReturning(“anyMethod()”) 
  41.     public void doAfterReturn(){ 
  42.         System.out.println(“后置通知”); 
  43.     }*/  
  44.     //得到方法的返回值  
  45.     @AfterReturning(pointcut=”anyMethod()”,returning=”result”)  
  46.     public void doAfterReturn(String result){  
  47.         System.out.println(“后置通知  “+result);  
  48.     }  
  49.       
  50.   
  51.     @After(“anyMethod()”)  
  52.     public void doAfter(){  
  53.         System.out.println(“最终通知”);  
  54.     }  
  55.       
  56. /*  @AfterThrowing(“anyMethod()”) 
  57.     public void doAfterThrow(){ 
  58.         System.out.println(“异常通知”); 
  59.     }*/  
  60.     @AfterThrowing(pointcut=”anyMethod()”,throwing=”e”)  
  61.     public void doAfterThrow(Exception e){  
  62.         System.out.println(“异常通知——“+e.getMessage());  
  63.     }  
  64.       
  65.     @Around(“anyMethod()”)  
  66.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  67.         System.out.println(“环绕通知  开始”);  
  68.         Object obj = pjp.proceed();  
  69.         System.out.println(“环绕通知  结束”);  
  70.         return obj;  
  71.     }  
  72. }  

(二)基于xml配置文件的
 




切面只是一个普通的javabean 



Java代码 

  1. import org.aspectj.lang.ProceedingJoinPoint;  
  2.   
  3. public class MyInterceptor1 {  
  4.       
  5.   
  6.     public void doAccessCheck(){  
  7.         System.out.println(“前置通知——-“);  
  8.     }  
  9.       
  10.     public void doAfterReturn(){  
  11.         System.out.println(“后置通知”);  
  12.     }  
  13.       
  14.   
  15.     public void doAfter(){  
  16.         System.out.println(“最终通知”);  
  17.     }  
  18.     public void doAfterThrow(){  
  19.         System.out.println(“异常通知”);  
  20.     }  
  21.       
  22.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  23.         System.out.println(“环绕通知  开始”);  
  24.         Object obj = pjp.proceed();  
  25.         System.out.println(“环绕通知  结束”);  
  26.         return obj;  
  27.     }  
  28. }  配置文件 : 

    Java代码 

    1. <?xml version=”1.0″ encoding=”UTF-8″?>  
    2. <beans xmlns=“http://www.springframework.org/schema/beans”  
    3.        xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
    4.        xmlns:context=“http://www.springframework.org/schema/context”   
    5.        xmlns:aop=“http://www.springframework.org/schema/aop”  
    6.        xmlns:tx=“http://www.springframework.org/schema/tx”  
    7.        xsi:schemaLocation=”http://www.springframework.org/schema/beans  
    8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    9.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
    10.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
    11.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd”>     
    12.        
    13.    
    14. [color=brown]     <bean id=“per” class=”com.mingbai.springaop.PersonServiceBean”/>  
    15.      <bean id=“myInterceptor” class=”com.mingbai.springaop.MyInterceptor1″/>  
    16.      <!–    
    17.      <aop:config>  
    18.         <aop:aspect id=“asp” ref=”myInterceptor”>  
    19.             <aop:pointcut id=“mycut” expression=”execution(* com.mingbai.springaop.*.*(..))”/>  
    20.             <aop:before pointcut-ref=“mycut” method=”doAccessCheck”/>  
    21.             <aop:after-returning pointcut-ref=“mycut” method=”doAfterReturn”/>  
    22.             <aop:after pointcut-ref=“mycut” method=”doAfter”/>  
    23.             <aop:after-throwing pointcut-ref=“mycut” method=”doAfterThrow”/>  
    24.             <aop:around pointcut-ref=“mycut” method=”doBasicProfiling”/>  
    25.         </aop:aspect>  
    26.      </aop:config>[/color]  
    27.      –>   
    28.      <!– 只是拦截返回类型为java.lang.String的方法     
    29.      <aop:config>  
    30.         <aop:aspect id=“asp” ref=”myInterceptor”>  
    31.             <aop:pointcut id=“mycut” expression=”execution(java.lang.String com.mingbai.springaop.*.*(..))”/>  
    32.             <aop:before pointcut-ref=“mycut” method=”doAccessCheck”/>  
    33.             <aop:after-returning pointcut-ref=“mycut” method=”doAfterReturn”/>  
    34.             <aop:after pointcut-ref=“mycut” method=”doAfter”/>  
    35.             <aop:after-throwing pointcut-ref=“mycut” method=”doAfterThrow”/>  
    36.             <aop:around pointcut-ref=“mycut” method=”doBasicProfiling”/>  
    37.         </aop:aspect>  
    38.      </aop:config>  
    39.    –>   
    40.    <!– 返回非void的方法 –>  
    41.    <aop:config>  
    42.         <aop:aspect id=“asp” ref=”myInterceptor”>  
    43.             <aop:pointcut id=“mycut” expression=”execution(!void com.mingbai.springaop.*.*(..))”/>  
    44.             <aop:before pointcut-ref=“mycut” method=”doAccessCheck”/>  
    45.             <aop:after-returning pointcut-ref=“mycut” method=”doAfterReturn”/>  
    46.             <aop:after pointcut-ref=“mycut” method=”doAfter”/>  
    47.             <aop:after-throwing pointcut-ref=“mycut” method=”doAfterThrow”/>  
    48.             <aop:around pointcut-ref=“mycut” method=”doBasicProfiling”/>  
    49.         </aop:aspect>  
    50.      </aop:config>  
    51.    <!– 匹配第一个参数为java.lang.String,其它的无所谓   
    52.      <aop:config>  
    53.         <aop:aspect id=“asp” ref=”myInterceptor”>  
    54.             <aop:pointcut id=“mycut” expression=”execution(* com.mingbai.springaop.*.*(..))”/>  
    55.             <aop:before pointcut-ref=“mycut” method=”doAccessCheck”/>  
    56.             <aop:after-returning pointcut-ref=“mycut” method=”doAfterReturn”/>  
    57.             <aop:after pointcut-ref=“mycut” method=”doAfter”/>  
    58.             <aop:after-throwing pointcut-ref=“mycut” method=”doAfterThrow”/>  
    59.             <aop:around pointcut-ref=“mycut” method=”doBasicProfiling”/>  
    60.         </aop:aspect>  
    61.      </aop:config>  
    62.    –>  
    63.      
    64. </beans>  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/100463.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • epplus 速度_【推荐套件】Excel利器 NPOI VS EPPLUS

    epplus 速度_【推荐套件】Excel利器 NPOI VS EPPLUS【工具】Excel利器—NPOIVSEPPLUS由于最近客户端不停抱怨,查询会宕机。其实,这算是老问题了,主要原因是:Query性能不佳由于该查询会使用到的Table事务量很大,容易会有Wait的现象到最后就TimeOut了。查询数据量太大,目前是放到DataSet之后直接用GridView绑定。其实,以上种种造成因素太多了。再加上,查询出来的结果使用端整批下载或是自订下载成Excel。最一开…

    2022年6月26日
    35
  • go语言后端框架2021_go语言编译器

    go语言后端框架2021_go语言编译器点击上方蓝色“飞雪无情”关注我,设个星标,第一时间看文章最近写了很多Go语言的原创文章,其中Go语言实战系列就有30篇,近15W字,还有最近更新的Go经典库系列、Gin实战系列,不过通过大…

    2022年10月12日
    3
  • Python修改文件后缀_python重命名文件名

    Python修改文件后缀_python重命名文件名例子,python批量修改文件后缀。代码:代码示例:importos,stringdefs_rename(path,old_ext,new_ext):for(path,dirs,files)inos.walk(path):forfilenameinfiles:ext=os.path.splitext(filename)[1]if(cmp(ext,old_ext)==0):newn…

    2025年12月9日
    3
  • “0x69ba3a96〞指令引用的〝0x00000000〞内存。该内存不能为〝written〞[通俗易懂]

    —转自sql1981(http://zhidao.baidu.com/question/385499006.html&__bd_tkn__=729142612d22862b5053a77a93ad20fd864f93af8078338d51fed8133ea5c69d362ad36bb4bcda3b39bb3949f6bbe47087ac3af56e60b1f4e7eb60157b54f836

    2022年4月9日
    57
  • ioctl函数详解(Linux内核 )

    ioctl函数详解(Linux内核 )1.概念ioctl是设备驱动程序中设备控制接口函数,一个字符设备驱动通常会实现设备打开、关闭、读、写等功能,在一些需要细分的情境下,如果需要扩展新的功能,通常以增设ioctl()命令的方式实现。在文件I/O中,ioctl扮演着重要角色,本文将以驱动开发为侧重点,从用户空间到内核空间纵向分析ioctl函数。2.用户空间ioctl#include<sys/ioctl.h>intioctl(intfd,intcmd,…);参数描述

    2022年10月17日
    3
  • 一个还不错的gridview 样式【Z】

    一个还不错的gridview 样式【Z】…

    2022年7月3日
    29

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号