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>  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Java private关键字及作用

    Java private关键字及作用private关键字使用场景:用private关键字将需要保护的成员变量进行修饰private关键字注意事项:一旦使用了private进行修饰,那么本类当中仍可以随意访问;但是超出本类范围就不可

  • 大数据spark、hadoop、hive、hbase面试题及解析[通俗易懂]

    大数据spark、hadoop、hive、hbase面试题及解析[通俗易懂](1)spark运行流程、源码架构(2)Hbase主键设计、hbase为何这么快?主键设计:1.生成随机数、hash、散列值2.字符串反转3.字符串拼接hbase为何快:https://blog.csdn.net/sghuu/article/details/102955969(3)Hbase读写流程,数据compact流程hbase读写流程:https://blog.csdn.n…

  • PHP 数组截取 array_slice() 函数

    PHP 数组截取 array_slice() 函数定义和用法array_slice()函数在数组中根据条件取出一段值,并返回。注释:如果数组有字符串键,所返回的数组将保留键名。(参见例子4)语法array_slice(array,offset,length,preserve)参数array必需。规定输入的数组。offset必需。数值。规定取出元素的开始位置。如果是正数,则从前往后开始取,如果是负值,从

  • JSONArray 转list

    JSONArray 转listJSONArray 转list 可以通过如下简便方法进行JSONArrayprogramsArray=jsonobject.getJSONArray(“programs”);Listlist=(List)JSONArray.toCollection(programsArray,Programs.class);转换过程中容易产生NoSuchMethodEx

  • slam关键技术_深度技术还做系统吗

    slam关键技术_深度技术还做系统吗本文由图像处理知识库整理SLAM(simultaneouslocalizationandmapping),也称为CML(ConcurrentMappingandLocali…

  • idea2021.7永久激活码【2021免费激活】

    (idea2021.7永久激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.htmlML…

发表回复

您的电子邮箱地址不会被公开。

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