< 1 > 配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" > <!-- 最簡單的注入 p 命名空間 --> <bean id="person" class="spring.beans.Person" p:personName="張三" p:personSex="男" p:personAge="1" ></bean> <!-- 自動生成所有被 @Aspect 標注修飾的類 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 設置 Spring 容器掃描哪些包下面的 JavaBean, * 代表所有包 --> <context:component-scan base-package="spring.beans"></context:component-scan> </beans>
< 二 > 切面類的定義 ( 前置通知: @Before, 后置通知: @After, 正常返回通知: @AfterReturning, 異常返回通知: @AfterThrowing)
package spring.beans; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect // 聲明切面類 @Component // 聲明是一個 JavaBean public class MyAop { // ( 方法執行前的方法 ) execution 是切點表達式 @Before("execution (public String spring.beans.User.toString())") public void before() { System.out.println("前置方法"); } // ( 方法執行后的方法 ) 注: 不管正常執行或者拋出異常 @After("execution (public String spring.beans.User.toString())") public void after() { System.out.println("后置處理"); } // ( 方法正確執行后的方法 ) 參數(切點表達式, 返回值變量) @AfterReturning(pointcut="execution (public String spring.beans.User.toString())", returning="result") public void returning(JoinPoint join, String result){ System.out.println("方法名: " + join.getSignature().getName()); System.out.println("結果為: " + result); }
// ( 方法拋出異常時執行的方法 ) 參數(切點表達式, 拋出的異常變量) @AfterThrowing(pointcut="execution (public String spring.beans.User.toString())", throwing="ex") public void throwing(JoinPoint join, Exception ex){ System.out.println(ex.getMessage()); } }
< 3 > 切面類的定義 ( 環繞通知: @Around ) 注: 功能強大但是不常用
package spring.beans; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect // 聲明切面類 @Component // 聲明是一個 JavaBean
@Order(0) // 聲明切面順序,越小優先級越高 public class MyAop_02 { // 環繞通知 @Around("execution (public String spring.beans.Person.toString())") public void around(ProceedingJoinPoint join){ try { System.out.println("方法調用之前"); String result = (String) join.proceed(); System.out.println("正常返回結果" + result); } catch (Throwable e) { System.out.println("出現了異常嘍"); e.printStackTrace(); } finally { System.out.println("方法調用之后"); } } }