Spring基础(17)——AOP——Throws Advice

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)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring使用AOP(面向切面编程)是一种可以在运行时动态将横切关注点与程序主逻辑进行解耦的技术。在Spring中,AOP可以帮助我们实现诸如日志记录、事务管理、性能监控等与业务逻辑无关的功能。 要在Spring中使用AOP,首先需要配置一个AOP代理,这个代理可以是基于Java动态代理(默认)或者CGLIB动态代理。然后,需要定义切入点(Pointcut)来确定哪些方法将会被拦截。接下来,定义通知(Advice)来指定在方法执行前、后或异常抛出时应该执行的逻辑。最后,将切入点和通知组合起来形成切面(Aspect),并将切面配置到Spring的配置文件中。 下面是一个简单的示例,展示了如何在Spring中使用AOP: 1. 配置AOP代理方式: ```xml <bean id="myTarget" class="com.example.MyTarget" /> <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <!-- 使用CGLIB动态代理 --> <aop:advisor advice-ref="myAdvice" pointcut="execution(* com.example.MyTarget.*(..))" /> </aop:aspect> </aop:config> ``` 2. 定义切面和通知: ```java public class MyAspect { public void beforeAdvice() { // 在方法执行前执行的逻辑 System.out.println("Before advice called."); } } public class MyAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { // 在方法执行前执行的逻辑 System.out.println("Before advice called."); } } ``` 在上述示例中,我们配置了一个名为`myTarget`的目标对象,并定义了一个名为`myAspectBean`的切面。切面中使用了`myAdvice`这个通知,并通过`pointcut`指定了拦截`com.example.MyTarget`类中的所有方法。当这些方法被调用时,通知中的逻辑将会在方法执行前执行。 请注意,以上示例只是一个简单的演示,并且配置方式和细节可能因实际需求而有所不同。Spring提供了更多灵活和强大的AOP功能,包括不同类型的切入点、多个通知的组合、异常处理等。你可以根据具体情况进行更详细的配置和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值