2.spring的aop之@AfterThrowing

定义组件切点

@Component
public class Chinese implements Person
{
<span style="white-space:pre">	</span>//实现Person接口的sayHello()方法
<span style="white-space:pre">	</span>public String sayHello(String name)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">	</span>//该方法体内虽然抛出了异常,但该方法
<span style="white-space:pre">	</span>//自己处理了该异常,所以AOP不会对该异常进行处理
<span style="white-space:pre">	</span>try
<span style="white-space:pre">	</span>{
<span style="white-space:pre">	</span>System.out.println("sayHello方法开始被执行...");
<span style="white-space:pre">	</span>new java.io.FileInputStream("a.txt");
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>catch (Exception ex)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">	</span>System.out.println("目标类的异常处理"
<span style="white-space:pre">	</span>+ ex.getMessage());
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>//返回简单的字符串
<span style="white-space:pre">	</span>return name + " Hello , Spring AOP";
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>//定义一个divide()方法
<span style="white-space:pre">	</span>public void divide()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">	</span>int a = 5 / 0;
<span style="white-space:pre">	</span>System.out.println("divide执行完成!");
<span style="white-space:pre">	</span>}
}

定义切面

@Aspect
public class AfterThrowingAdviceTest {
<span style="white-space:pre">	</span>// 匹配org.crazyit.app.service.impl包下所有类的、
<span style="white-space:pre">	</span>// 所有方法的执行作为切入点
<span style="white-space:pre">	</span>@AfterThrowing(throwing = "ex", pointcut = "execution(* org.crazyit.app.service.impl.*.*(..))")
<span style="white-space:pre">	</span>public void doRecoveryActions(Throwable ex) {
<span style="white-space:pre">	</span>System.out.println("目标方法中抛出的异常:" + ex);
<span style="white-space:pre">	</span>System.out.println("模拟抛出异常后的增强处理...");
<span style="white-space:pre">	</span>}
}
交给spring管理

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre">	</span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre">	</span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre">	</span>xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre">	</span>xsi:schemaLocation="http://www.springframework.org/schema/beans 
<span style="white-space:pre">	</span>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
<span style="white-space:pre">	</span>http://www.springframework.org/schema/context
<span style="white-space:pre">	</span>http://www.springframework.org/schema/context/spring-context-3.0.xsd
<span style="white-space:pre">	</span>http://www.springframework.org/schema/aop
<span style="white-space:pre">	</span>http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<span style="white-space:pre">	</span><!-- 指定自动搜索Bean组件、自动搜索切面类 -->
<span style="white-space:pre">	</span><context:component-scan base-package="org.crazyit.app.service
<span style="white-space:pre">	</span>,org.crazyit.app.advice">
<span style="white-space:pre">	</span><context:include-filter type="annotation"
<span style="white-space:pre">	</span>expression="org.aspectj.lang.annotation.Aspect"/>
<span style="white-space:pre">	</span></context:component-scan>
<span style="white-space:pre">	</span><!-- 启动@AspectJ支持 -->
<span style="white-space:pre">	</span><aop:aspectj-autoproxy/>
</beans>
简单测试

public class BeanTest {
<span style="white-space:pre">	</span>public static void main(String[] args) throws Exception {
<span style="white-space:pre">	</span>// 创建Spring容器
<span style="white-space:pre">	</span>ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
<span style="white-space:pre">	</span>Person p = (Person) ctx.getBean("chinese", Person.class);
<span style="white-space:pre">	</span>System.out.println(p.sayHello("张三"));
<span style="white-space:pre">	</span>p.divide();
<span style="white-space:pre">	</span>}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值