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) 演绎环绕通知”

猜你喜欢

转载自blog.csdn.net/haidaoxianzi/article/details/80638608