笔记13 AOP中After和AfterReturning的区别

笔记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.结果

 

posted @ 2018-04-22 14:23 雨落忧伤- 阅读( ...) 评论( ...) 编辑 收藏
AOP(面向切面编程),@After和@AfterReturning是两个常用的注解,用于在目标方法执行后执行一些逻辑。它们的区别如下: 1. @After注解: - @After注解标记的方法会在目标方法执行后无论是否发生异常都会被执行。 - 它类似于finally代码块,无论目标方法是否成功执行,都会执行@After注解标记的方法。 - 通常用于释放资源或进行清理工作。 2. @AfterReturning注解: - @AfterReturning注解标记的方法只会在目标方法成功执行后被执行,不会在目标方法发生异常时执行。 - 它可以获取到目标方法的返回值作为参数。 - 通常用于处理目标方法的返回结果。 下面是一个示例: ```java @Aspect @Component public class LoggingAspect { @After(value = "execution(* com.example.MyService.doSomething(..))") public void afterMethod(JoinPoint joinPoint) { System.out.println("执行目标方法后执行@After注解标记的方法"); } @AfterReturning(value = "execution(* com.example.MyService.doSomething(..))", returning = "result") public void afterReturningMethod(JoinPoint joinPoint, Object result) { System.out.println("目标方法返回值:" + result); System.out.println("执行目标方法成功后执行@AfterReturning注解标记的方法"); } } ``` 在上述示例,`@After`注解标记的`afterMethod`方法会在目标方法`doSomething`执行后无论是否发生异常都会被执行。而`@AfterReturning`注解标记的`afterReturningMethod`方法只会在目标方法执行成功并且返回结果时执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值