定义组件切点
@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>}
}