1、异常发生的时候,通知某个服务对象做处理

2、实现throwsAdvice接口

接口实现:

public interface IHello {
public void sayHello(String str) throws Exception;
}
public class Hello implements IHello {
@Override
public void sayHello(String str) throws Exception {
System.out.println("你好"+str);
//测试抛出异常
throw new Exception("故意造成异常!");
}
}

 SayThrowAdvice文件:

public class SayThrowAdvice implements ThrowsAdvice {

	public void afterThrowing(Method method,Object[] objs,Object target,Throwable ta)
{
System.out.println("我捕获的异常是-> "+ta+" =====, 在方法 "+method+"抛出了异常");
}
}

  

applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 建立目标对象实例 -->
<bean id="bean_hello" class="com.pb.Hello" />
<!-- 创建执行前advice实例 -->
<bean id="sba" class="com.pb.SayBeforeAdvice" />
<!-- 创建执行后advice实例 -->
<bean id="sfa" class="com.pb.SayAfterAdvice" />
<!-- 创建Around advice实例 -->
<bean id="ssd" class="com.pb.SayAroundAdvice" />
<!-- 创建Throw advice实例 -->
<bean id="sta" class="com.pb.SayThrowAdvice" />
<!-- 建立代理对象 -->
<bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置代理的接口 -->
<property name="proxyInterfaces">
<value>com.pb.IHello</value>
</property>
<!-- 设置目标对象实例 -->
<property name="target">
<ref bean="bean_hello"/>
</property>
<!-- 设置Advice实例 -->
<property name="interceptorNames">
<list>
<!--
<value>sba</value>
<value>sfa</value>
-->
<value>ssd</value>
<value>sta</value>
</list>
</property>
</bean>
</beans>

 主方法文件:

public class MainTest {

	public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); IHello hello=(IHello)context.getBean("helloProxy"); try {
hello.sayHello("访客");
} catch (Exception e) {
// TODO: handle exception
} } }

 执行效果:

Spring AOP创建Throwdvice实例的更多相关文章

  1. Spring AOP创建AroundAdvice实例

    AroundAdvice 1.在方法之前和之后来执行相应的操作 2.实现MethodInterceptor接口 接口文件: public interface IHello { public void ...

  2. spring AOP的注解实例

    上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...

  3. 阿里四面:你知道Spring AOP创建Proxy的过程吗?

    Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...

  4. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

  5. Spring AOP创建BeforeAdvice和AfterAdvice实例

    BeforeAdvice 1.会在目标对象的方法执行之前被调用. 2.通过实现MethodBeforeAdvice接口来实现. 3.该接口中定义了一个方法即before方法,before方法会在目标对 ...

  6. Spring AOP 创建增强类

    AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的增强:     1)前置增强:org.springframework.aop.BeforeAd ...

  7. Spring AOP 创建切面

        增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...

  8. spring aop简单日志实例

    转载自:http://www.blogjava.net/laoding/articles/242611.html 一直就用spring的IOC,遗憾spring的另一重要组成部分AOP却没用过,所以近 ...

  9. Spring AOP前置通知实例说明AOP相关概念

    今天又看了下韩顺平的SpringAOP的讲解,讲解的很透彻.仿照视频自己使用下前置通知. 一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Ser ...

随机推荐

  1. USACO 重排干草&&BZOJ1045

    USACO 重排干草&&BZOJ1045 Description 约翰订购了很多干草,他在农场里标记了 N 个位置.这些位置近似地构成一个圆环.他原打算 让送货司机在 i 号位卸下 B ...

  2. semantic ui要装什么才能使用

    作者:呆呆笨笨链接:https://www.zhihu.com/question/32233356/answer/196799506来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  3. ubuntu之路——day17.2 RGB图像的卷积、多个filter的输出、单个卷积层的标记方法

    和单层图像的卷积类似,只需要对每一个filter构成的三层立方体上的每一个数字与原图像对应位置的数字相乘相加求和即可. 在这个时候可以分别设置filter的R.G.B三层,可以同时检测纵向或横向边缘, ...

  4. Net core学习系列(三)——Net Core中的依赖注入

    本文通过一个维修工与工具库的例子形象的描述一下为什么要用依赖注入.它的工作原理是什么样的, 然后根据这个类比一下ASP.NET Core 中的依赖注入, 从而深刻了解它的使用方法.注意事项以及回收机制 ...

  5. 请写出css3样式的优先级,并排序

    !important(权重最大)1000>内嵌样式(style="")>内部样式>外联样式>@import url("url");

  6. Centos7搭建FTP服务详细过程

    Centos7搭建FTP服务详细过程https://blog.csdn.net/sinat_30802291/article/details/81706152

  7. shell关闭指定进程

    例如要关闭jupyter-notebook这个进程: ps -ef | grep jupyter-notebook | grep -v grep | cut -c 9-15 | xargs kill ...

  8. 微信公众号开发系统入门教程(公众号注册、开发环境搭建、access_token管理、Demo实现、natapp外网穿透)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/a1786223749/article/ ...

  9. Sword 第三方库介绍二

    /* uuid生成 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <ass ...

  10. C语言 sscanf函数补充

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h&g ...