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

Spring AOP 實戰:AspectJ 註解與 XML 兩種配置方式

     編輯:Java 相關知識

方式一:AspectJ 註解配置

1. 啟用註解支持

<aop:aspectj-autoproxy/>

2. 切面類

@Aspect
public class SecurityHandler {
    @Pointcut("execution(* add*(..))")
    private void addMethod() {}

    @After("addMethod()")
    private void checkSecurity() {
        System.out.println("【安全檢查】");
    }
}

3. 目標 Bean

<bean id="userManager" class="com.jadyer.annotation.UserManagerImpl"/>
<bean id="securityHandler" class="com.jadyer.annotation.SecurityHandler"/>

方式二:XML 配置(CGLIB 代理)

1. 強制 CGLIB

<aop:config proxy-target-class="true">
    <aop:aspect id="securityAspect" ref="securityHandler">
        <aop:pointcut id="addMethod" expression="execution(* add*(..))"/>
        <aop:before method="checkSecurity" pointcut-ref="addMethod"/>
    </aop:aspect>
</aop:config>

2. 切面類無需註解

public class SecurityHandler {
    public void checkSecurity(JoinPoint jp) {
        System.out.println("方法:" + jp.getSignature().getName());
        for (Object arg : jp.getArgs()) System.out.println("參數:" + arg);
        System.out.println("【安全檢查】");
    }
}

代理機制對比

維度JDK 動態代理CGLIB
前提目標實現接口無需接口,繼承目標類
原理接口實現字節碼生成子類
限制無法代理非接口方法不能代理 final 方法/類

選型建議

  • 業務類均實現接口 → 默認 JDK 代理
  • 無接口或需代理具體類方法 → 配置 proxy-target-class="true" 強制 CGLIB
  • Spring 會自動判斷:有接口用 JDK,無接口用 CGLIB

切點表達式速查

execution(* add*(..))                    // 所有 add 開頭方法
execution(* com.pkg.service.*.*(..))     // service 包所有方法
execution(* *.add*(..)) || execution(* *.del*(..)) // add 或 del 開頭
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved