• 笔记13 AOP中After和AfterReturning的区别


    AOP中 @Before @After @AfterThrowing@AfterReturning的执行顺序

     1 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
     2    Object result;
     3    try {
     4        //@Before
     5        result = method.invoke(target, args);
     6        //@After
     7        return result;
     8    } catch (InvocationTargetException e) {
     9        Throwable targetException = e.getTargetException();
    10        //@AfterThrowing
    11        throw targetException;
    12    } finally {
    13        //@AfterReturning
    14    }
    15 }

    以Audience为例,代码如下:

     1 package concert;
     2 
     3 import org.aspectj.lang.annotation.After;
     4 import org.aspectj.lang.annotation.AfterReturning;
     5 import org.aspectj.lang.annotation.AfterThrowing;
     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
    12 @Component
    13 public class Audience {
    14     @Pointcut("execution(* concert.Performance.perform(..))")
    15     public void performs() {
    16 
    17     }
    18 
    19     @Before("performs()")
    20     public void silenceCellPhones() {
    21         System.out.println("Silencing cell phone");
    22     }
    23 
    24     @Before("performs()")
    25     public void takeSeats() {
    26         System.out.println("Taking seats");
    27     }
    28 
    29     @After("performs()")
    30     public void after() {
    31         System.out.println("after");
    32     }
    33 
    34     @AfterReturning("performs()")
    35     public void applause() {
    36         System.out.println("CLAP CLAP CLAP");
    37     }
    38 
    39     @AfterThrowing("performs()")
    40     public void demandRefund() {
    41         System.out.println("Demanding a refund");
    42     }
    43 }

    执行结果:

    注入AspectJ切面 (新)

    1.将原来的观众类定义为一个真正的切面,Audience.java 将观众的行为都放在这个切面中,然后在spring中引用这个切面,并且为这个切面注入新的方法,具体可参照笔记12。

     1 package concert4;
     2 
     3 public aspect Audience {
     4     public Audience(){}
     5     pointcut performance():execution(* perform(..));
     6     
     7     before():performance(){
     8         System.out.println("Silencing cell phone");
     9     }
    10     before():performance(){
    11         System.out.println("Taking seats");
    12     }
    13     after():performance(){
    14         System.out.println("CLAP CLAP CLAP");
    15     }
    16     after()returning :performance(){
    17         System.out.println("--------------评论----------------");
    18         System.out.println(criticismEngine.getCriticism());
    19         System.out.println("----------------------------------");
    20     }
    21     
    22     private CriticismEngine criticismEngine;
    23     
    24     public void setCriticismEngine(CriticismEngine criticismEngine){
    25         this.criticismEngine=criticismEngine;
    26     }
    27 }

    2.修改XML配置文件

     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:aop="http://www.springframework.org/schema/aop"
     5     xmlns:tx="http://www.springframework.org/schema/tx"
     6     xmlns:context="http://www.springframework.org/schema/context"
     7     xmlns:c="http://www.springframework.org/schema/c"
     8     xsi:schemaLocation="
     9    http://www.springframework.org/schema/beans
    10    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    11    http://www.springframework.org/schema/aop
    12    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    13    http://www.springframework.org/schema/tx
    14    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    15    http://www.springframework.org/schema/context     
    16    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    17            
    18            <bean class="concert4.Classcial"></bean>
    19         <bean id="criticismEngine" class="concert4.CriticismEngineTmpl">
    20             <property name="criticismPool">
    21                 <list>
    22                     <value>不好</value>
    23                     <value>很不好</value>
    24                     <value>非常不好</value>
    25                 </list>
    26             </property>
    27         </bean>
    28         <bean id="audience" class="concert4.Audience" factory-method="aspectOf">
    29             <property name="criticismEngine" ref="criticismEngine"></property>
    30         </bean>
    31             <aop:config>
    32                    <aop:aspect ref="audience">                                         
    33                        <aop:declare-parents types-matching="concert4.Performance+" 
    34                                             implement-interface="concert4.Encoreable" 
    35                                             default-impl="concert4.DefaultEncoreable"/>
    36                    </aop:aspect>
    37            </aop:config>
    38              
    39    </beans>

    3.结果

  • 相关阅读:
    如何在spring框架中解决多数据源的问题
    Spring动态配置多数据源
    SSH配置动态数据源
    Js操作Select大全
    SSH自定义分页标签
    兼容ie[6-9]、火狐、Chrome、opera、maxthon3、360浏览器的js本地图片预览
    Hibernate关联关系配置(一对多、一对一和多对多)
    SSH整合配置二级缓存
    CKEditor实现图片上传
    ckeditor的详细配置
  • 原文地址:https://www.cnblogs.com/lyj-gyq/p/8907132.html
Copyright © 2020-2023  润新知