Spring AOP-04-异常抛出增强 ThrowsAdvice

package com.test.springadvicetype;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 服务员类
 */
@Component
public class Waiter {
    /**
     * 服务
     * @param name
     */
    public String serve(String name) {
        System.out.println(name + ",您好,很高兴为您服务。");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return name + ",您好,现在是北京时间" + format.format(new Date());
    }

    /**
     * 开车
     * @param name
     */
    public void driving(String name) {
        throw new RuntimeException(name + ",您好,禁止酒后驾车!");
    }
}

2、异常抛出增强,实现 ThrowsAdvice 接口

package com.test.springadvicetype.throwsadvice;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.stereotype.Component;

/**
 * 异常抛出增强
 */
@Component
public class SpringThrowsAdvice implements ThrowsAdvice {

    public void afterThrowing(Exception e)  throws  Throwable{
        System.out.printf("异常抛出增强执行:%s%n", e);
    }
}

3、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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.test.springadvicetype"/>
</beans>

4、测试代码

package com.test.springadvicetype.throwsadvice;

import com.test.springadvicetype.Waiter;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring异常抛出增强测试
 */
public class SpringThrowsAdviceDemo {

    public static void main(String[] args) {
        System.out.println("Spring异常抛出增强测试");
        System.out.println("==========我是分割线==========");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-springaoptype.xml");
        Waiter waiter = (Waiter) context.getBean("waiter");
        SpringThrowsAdvice advice = (SpringThrowsAdvice) context.getBean("springThrowsAdvice");
        //Spring提供的代理工厂
        ProxyFactory pf = new ProxyFactory();
        //设置代理目标
        pf.setTarget(waiter);
        pf.addAdvice(advice);
        //生成代理实例
        Waiter proxy = (Waiter)pf.getProxy();
        proxy.driving("Michael");
    }
}

5、测试结果

Spring异常抛出增强测试
==========我是分割线==========
异常抛出增强执行:java.lang.RuntimeException: Michael,您好,禁止酒后驾车!
Exception in thread "main" java.lang.RuntimeException: Michael,您好,禁止酒后驾车!
	at com.test.springadvicetype.Waiter.driving(Waiter.java:30)
	at com.test.springadvicetype.Waiter$$FastClassBySpringCGLIB$$57d4d7c2.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:112)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
	at com.test.springadvicetype.Waiter$$EnhancerBySpringCGLIB$$e5915ab1.driving(<generated>)
	at com.test.springadvicetype.throwsadvice.SpringThrowsAdviceDemo.main(SpringThrowsAdviceDemo.java:28)