Spring AOP创建Throwdvice实例
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实例的更多相关文章
- Spring AOP创建AroundAdvice实例
AroundAdvice 1.在方法之前和之后来执行相应的操作 2.实现MethodInterceptor接口 接口文件: public interface IHello { public void ...
- spring AOP的注解实例
上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...
- 阿里四面:你知道Spring AOP创建Proxy的过程吗?
Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...
- Spring学习(十八)----- Spring AOP+AspectJ注解实例
我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...
- Spring AOP创建BeforeAdvice和AfterAdvice实例
BeforeAdvice 1.会在目标对象的方法执行之前被调用. 2.通过实现MethodBeforeAdvice接口来实现. 3.该接口中定义了一个方法即before方法,before方法会在目标对 ...
- Spring AOP 创建增强类
AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的增强: 1)前置增强:org.springframework.aop.BeforeAd ...
- Spring AOP 创建切面
增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...
- spring aop简单日志实例
转载自:http://www.blogjava.net/laoding/articles/242611.html 一直就用spring的IOC,遗憾spring的另一重要组成部分AOP却没用过,所以近 ...
- Spring AOP前置通知实例说明AOP相关概念
今天又看了下韩顺平的SpringAOP的讲解,讲解的很透彻.仿照视频自己使用下前置通知. 一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Ser ...
随机推荐
- iOS9 TableView和ScrollView滚动不响应问题
今天开始适配iOS9,发现在有UITableView或是ScrollView的界面滚动不灵敏有些卡顿,研究了下是因为tableview的cell里有可以相应点击事件的Button,当我点击到Butto ...
- 【mybatis源码学习】mybatis的反射模块
一.重要的类和接口 org.apache.ibatis.reflection.MetaClass//对于javaBean的calss类进行反射操作的代理类(获取属性的类型,获取属性的get,set方法 ...
- c 越界
int main(int argc, char* argv[]){ int i = 0; int arr[3] = {0}; for(; i<=3; i++){ arr[i] = 0; prin ...
- ERROR: relation "pg_buffercache" does not exist
创建pg_buffercache后,查询时报错: postgres=# create extension pg_buffercache; postgres=# select * from pg_buf ...
- Leetcode: Split BST
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...
- IDEA 如何搭建maven 安装、下载、配置(图文)
1.下载 maven 压缩包 输入网址 www.apache.org 会看到以下界面 住下划看到以下界面 然后看到这个界面 选择下载这个版本 下载后 解压 maven 压缩包 (文件路径不建议用有中文 ...
- 全基因组关联分析(GWAS)的计算原理
前言 关于全基因组关联分析(GWAS)原理的资料,网上有很多. 这也是我写了这么多GWAS的软件教程,却从来没有写过GWAS计算原理的原因. 恰巧之前微博上某位小可爱提问能否写一下GWAS的计算原理. ...
- springboot:redis反序列化发生类型转换错误
明明是同一个类,在反序列时报类型转换错误,真实奇怪.经查找资料,说是引入了devtools的缘故. 注释掉以下内容: <dependency> <groupId>org.spr ...
- WebViewJavascriptBridge js跟app的交互框架
https://github.com/marcuswestin/WebViewJavascriptBridge 参考: https://www.cnblogs.com/LiLihongqiang/p/ ...
- [LeetCode] 271. Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...