spring-Aop by aspectj pojo+xml (1) 演繹前置後置異常通知

aspectj通過 pojo+xml演繹spring Aop的前置,後置,異常通知。下面是測試樣例:

1 定義接口

package com.msyd.spring.aop.pojoxml;
public interface Performer {
    public void perform();
}

2 定義目標對象

package com.msyd.spring.aop.pojoxml;
public class Singer implements Performer {
    public void perform() {
        System.out.println("i am a singer, i speak for myself !");
        String ss = null;
        ss.length();
    }
}

3 定義切面對象

package com.msyd.spring.aop.pojoxml;
import org.aspectj.lang.JoinPoint;
public class Audience {
    public void takeSeat() {
        System.out.println("takeSeat");
    }
    public void turnOff() {
        System.out.println("turnOff");
    }
    public void Applaud(JoinPoint jp) {
        System.out.println("Applaud");
    }
    public void payOff(Exception e) {
        System.out.println("payOff");
    }
    public void goHome() {
        System.out.println("goHome");
    }

}

4 定義xml

<?xml version="1.0"?>
<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"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <!-- 觀衆:切面 -->
  <bean id="audience" class="com.msyd.spring.aop.pojoxml.Audience"/>

<!-- 目標對象 -->
<bean id="singer" class="com.msyd.spring.aop.pojoxml.Singer"/>

<aop:config>
   <aop:aspect id="audienceAspect" ref="audience">
       <aop:pointcut expression="execution(* *..Performer.perform())" id="audiencePointcut"/>
       <aop:before method="takeSeat" pointcut-ref="audiencePointcut"/>
       <aop:before method="turnOff" pointcut-ref="audiencePointcut"/>
       <aop:after-returning method="Applaud" returning="ret" pointcut-ref="audiencePointcut"/>
       <aop:after-throwing method="payOff" throwing="e" pointcut-ref="audiencePointcut"/>
       <aop:after method="goHome" pointcut-ref="audiencePointcut"/>
   </aop:aspect>

</aop:config>

</beans>

5 寫app 測試

package com.msyd.spring.aop.pojoxml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("pojoxml.xml", App.class);
        Performer P = (Performer) ac.getBean("singer");
        P.perform();
    }
}

6 測試結果

-- 異常通知結果:
Exception in thread "main" java.lang.NullPointerException
    at com.msyd.spring.aop.pojoxml.Singer.perform(Singer.java:8)
    ......
takeSeat
turnOff
i am a singer, i speak for myself !
payOff
goHome
-- 正常結果(把Singer裏面的異常去掉):
takeSeat
turnOff
i am a singer, i speak for myself !
Applaud
goHome

以上是pojo+xml的前置後置異常通知測試樣例,下一節我們會共同探討aspectJ 的 pojo+xml環繞通知測試樣例,博文title: “spring-Aop by aspectj pojo+xml (2) 演繹環繞通知”

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章