Throws Advice:
指异常通知,用来处理在方法调用过程中产生的错误或抛出的异常。ThrowsAdvice定义在异常发生时该有什么动作。ThrowsAdvice是一个标识接口,它没有定义任何实现的方法。但是,实现这个接口的类必须至少有个如下方法:
void afterThrowing(Throwable throwable)
package test.advice;
public class BusinessCode {
public void dosomething(String something) {
throw new RuntimeException("运行时异常:" + something);
}
}
package test.advice;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class ExceptionAdvice implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target, Exception ex) {
System.out.println("method:" + method.getName());
System.out.println("异常:" + ex.getMessage());
}
}
package test.advice;
import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// Reception target = new ConcreteReception();
// BeforeAdvice advice = new GreetingBeforeAdvice();
// ProxyFactory pf = new ProxyFactory();
// pf.setTarget(target);
// pf.addAdvice(advice);
// Reception proxy = (Reception) pf.getProxy();
// proxy.serveCustomer("HeMaSoft.com");
ApplicationContext context = new ClassPathXmlApplicationContext("spring-servlet.xml");
BusinessCode reception = (BusinessCode) context.getBean("reception");
reception.dosomething("Test");
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="greetingBeforeAdvice"
class="test.advice.GreetingBeforeAdvice">
</bean>
<bean id="greetingAroundAdvice"
class="test.advice.GreetingAroundAdvice">
</bean>
<bean id="exceptionAdvice" class="test.advice.ExceptionAdvice" />
<bean id="reception"
class="org.springframework.aop.framework.ProxyFactoryBean">
<!--
<property name="proxyInterfaces"
value="test.advice.Reception"></property>
-->
<property name="proxyTargetClass" value="true" />
<property name="interceptorNames">
<list>
<!-- <value>greetingAfterAdvice</value>
<value>greetingBeforeAdvice</value> -->
<value>exceptionAdvice</value>
</list>
</property>
<property name="target" ref="target"></property>
</bean>
<bean id="target" class="test.advice.BusinessCode"></bean>
<bean id="greetingAfterAdvice"
class="test.advice.GreetingAfterAdvice">
</bean>
</beans>
void afterThrowing(Method method, Object [] args, Object target, Throwable throwable)
七月 08, 2018 1:36:49 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5a10411: startup date [Sun Jul 08 01:36:49 CST 2018]; root of context hierarchy
七月 08, 2018 1:36:49 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-servlet.xml]
method:dosomething
异常:运行时异常:Test
Exception in thread "main" java.lang.RuntimeException: 运行时异常:Test
at test.advice.BusinessCode.dosomething(BusinessCode.java:5)
at test.advice.BusinessCode$$FastClassBySpringCGLIB$$3bf8a3c7.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:125)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at test.advice.BusinessCode$$EnhancerBySpringCGLIB$$6b0b17a2.dosomething(<generated>)
at test.advice.Test.main(Test.java:20)