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

 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result;
try {
//@Before
result = method.invoke(target, args);
//@After
return result;
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
//@AfterThrowing
throw targetException;
} finally {
//@AfterReturning
}
}

以Audience为例,代码如下:

 package concert;

 import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class Audience {
@Pointcut("execution(* concert.Performance.perform(..))")
public void performs() { } @Before("performs()")
public void silenceCellPhones() {
System.out.println("Silencing cell phone");
} @Before("performs()")
public void takeSeats() {
System.out.println("Taking seats");
} @After("performs()")
public void after() {
System.out.println("after");
} @AfterReturning("performs()")
public void applause() {
System.out.println("CLAP CLAP CLAP");
} @AfterThrowing("performs()")
public void demandRefund() {
System.out.println("Demanding a refund");
}
}

执行结果:

笔记13 AOP中After和AfterReturning的区别-LMLPHP

注入AspectJ切面 (新)

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

 package concert4;

 public aspect Audience {
public Audience(){}
pointcut performance():execution(* perform(..)); before():performance(){
System.out.println("Silencing cell phone");
}
before():performance(){
System.out.println("Taking seats");
}
after():performance(){
System.out.println("CLAP CLAP CLAP");
}
after()returning :performance(){
System.out.println("--------------评论----------------");
System.out.println(criticismEngine.getCriticism());
System.out.println("----------------------------------");
} private CriticismEngine criticismEngine; public void setCriticismEngine(CriticismEngine criticismEngine){
this.criticismEngine=criticismEngine;
}
}

2.修改XML配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean class="concert4.Classcial"></bean>
<bean id="criticismEngine" class="concert4.CriticismEngineTmpl">
<property name="criticismPool">
<list>
<value>不好</value>
<value>很不好</value>
<value>非常不好</value>
</list>
</property>
</bean>
<bean id="audience" class="concert4.Audience" factory-method="aspectOf">
<property name="criticismEngine" ref="criticismEngine"></property>
</bean>
<aop:config>
<aop:aspect ref="audience">
<aop:declare-parents types-matching="concert4.Performance+"
implement-interface="concert4.Encoreable"
default-impl="concert4.DefaultEncoreable"/>
</aop:aspect>
</aop:config> </beans>

3.结果

笔记13 AOP中After和AfterReturning的区别-LMLPHP

04-27 05:22