首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Spring AOP XML 命名空間配置詳解(一):環境建置與首個攔截範例

     編輯:Java 相關知識

前言

專案若使用 JDK 1.4 或不想用註解,可改用 Spring XML Schema-based AOP 命名空間,功能與 @AspectJ 等價,配置移至 XML。

1. 引入 AOP 命名空間

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="https://www.springframework.org/schema/aop"
       xsi:schemaLocation="
         https://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans-2.5.xsd
         https://www.springframework.org/schema/aop
         https://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  ... 
</beans>

2. 核心標籤 <aop:config>

所有 AOP 配置皆為其子標籤。本範例攔截 People.sayHello() 並在執行前輸出日誌。

3. 目標類 People

public class People {
    public String sayHello(String str) {
        System.out.println(this.getClass().getName() + "說:" + str);
        return str;
    }
}

4. 切面類 MyAspect

public class MyAspect {
    public void beforeAdvice(JoinPoint point) {
        System.out.println("前置通知被觸發:" +
            point.getTarget().getClass().getName() + "將要" + point.getSignature().getName());
    }
}

5. 完整 Spring XML 配置

<bean id="MyAspect" class="aop.test.MyAspect"/>
<bean id="TestBean" class="aop.test.People"/>

<aop:config proxy-target-class="true">
  <aop:aspect ref="MyAspect" order="0" id="Test">
    <aop:pointcut id="testPointcut" expression="execution(* aop..*(..))"/>
    <aop:before pointcut-ref="testPointcut" method="beforeAdvice"/>
  </aop:aspect>
</aop:config>

6. 測試類與執行結果

public class TestMain {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        People p = (People) ac.getBean("TestBean"); // 實際拿到的是代理物件
        p.sayHello("傳入的參數值");
    }
}

Console 輸出:

前置通知被觸發:aop.test.People將要sayHello
aop.test.People說:傳入的參數值

關鍵點

  • proxy-target-class="true":強制使用 CGLIB 代理(針對類而非介面)。
  • expression="execution(* aop..*(..))":攔截 aop 套件下所有類別的所有方法。
  • 從 ProxyFactoryBean 取得 Bean 即為代理物件,透明植入切面邏輯。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved