spring AOP (使用AspectJ的注解方式 的aop實現) (6)


前面的動態代理中,可以有前置通知,后置通知,返回通知,異常通知

在spring2.0以后,可以使用基於 AspectJ 注解或基於 XML 配置的 AOP

  • AspectJ:Java 社區里最完整最流行的 AOP 框架.

一、在 Spring 中啟用 AspectJ 注解支持

  • 1要在 Spring 應用中使用 AspectJ 注解, 必須在 classpath 下包含 AspectJ 類庫: aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar
    maven 引入
<dependency>
    <groupId>org.springframework</groupId>  
    <artifactId>spring-aop</artifactId>  
    <version>4.1.9.RELEASE</version>  
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.1.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>  
    <artifactId>aspectjrt</artifactId>  
    <version>1.6.8</version>  
</dependency>  
<dependency>  
    <groupId>org.aspectj</groupId>  
    <artifactId>aspectjweaver</artifactId>  
    <version>1.6.8</version>  
    <scope>runtime</scope>  
</dependency>    
  • 2將 aop Schema 添加到 根元素中.
  • 3要在 Spring IOC 容器中啟用 AspectJ 注解支持, 只要在 Bean 配置文件中定義一個空的 XML 元素
<!-- 配置自動為匹配 aspectJ 注解的 Java 類生成代理對象 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
  • 4當 Spring IOC 容器偵測到 Bean 配置文件中的 aop:aspectj-autoproxy 元素時, 會自動為與 AspectJ 切面匹配的 Bean 創建代理.

二、AspectJ 支持 5 種類型的通知注解:

  • @Before: 前置通知, 在方法執行之前執行
  • @After: 后置通知, 在方法執行之后執行
  • @AfterRunning: 返回通知, 在方法返回結果之后執行
  • @AfterThrowing: 異常通知, 在方法拋出異常之后
  • @Around: 環繞通知, 圍繞着方法執行

2.1、使用之前的 計算器接口和實現類 ArithmeticCalculator.java , ArithmeticCalculatorImpl.java

@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator{}  

2.2、在xml中增加掃描注解和aspectj的支持

<?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        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.1.xsd">
        
    <!-- 配置自動掃描的包 -->
    <context:component-scan base-package="com.hp.spring.aop.annotation"></context:component-scan>
    
    <!-- 配置自動為匹配 aspectJ 注解的 Java 類生成代理對象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>  

2.3、編寫切面類,定義各種通知

@Aspect //注解定義切面
@Component
public class LoggingAspect {
    //前置通知
    @Before("execution(public int com.hp.spring.aop.annatation.ArithmeticCalculator.*(int, int))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object [] args = joinPoint.getArgs();
        
        System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
    }
    //前置通知
    @After("execution(* com.hp.spring.aop.annatation.*.*(..))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends");
    }
    
}  

由於@before 和@after的表達式都一樣,所以spring支持對表達式進行抽取成一個方法,抽取后的代碼如下:

package com.hp.spring.aop.annotation;
import java.util.Arrays;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
 * 可以使用 @Order 注解指定切面的優先級, 值越小優先級越高
 */
@Order(2) 
@Aspect
@Component
public class LoggingAspect {
    
    /**
     * 定義一個方法, 用於聲明切入點表達式. 一般地, 該方法中再不需要添入其他的代碼. 
     * 使用 @Pointcut 來聲明切入點表達式. 
     * 后面的其他通知直接使用方法名來引用當前的切入點表達式. 
     */
    @Pointcut("execution(public int com.hp.spring.aop.annotation.ArithmeticCalculator.*(..))")
    public void declareJointPointExpression(){}
    
    /**
     * 在 com.hp.spring.aop.annotation.ArithmeticCalculator 接口的每一個實現類的每一個方法開始之前執行一段代碼
     */
    @Before("declareJointPointExpression()")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object [] args = joinPoint.getArgs();
        
        System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
    }
    
    /**
     * 在方法執行之后執行的代碼. 無論該方法是否出現異常
     */
    @After("declareJointPointExpression()")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends");
    }
    
    /**
     * 在方法法正常結束受執行的代碼
     * 返回通知是可以訪問到方法的返回值的!
     */
    @AfterReturning(value="declareJointPointExpression()",
            returning="result")
    public void afterReturning(JoinPoint joinPoint, Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends with " + result);
    }
    
    /**
     * 在目標方法出現異常時會執行的代碼.
     * 可以訪問到異常對象; 且可以指定在出現特定異常時在執行通知代碼
     */
    @AfterThrowing(value="declareJointPointExpression()",
            throwing="e")
    public void afterThrowing(JoinPoint joinPoint, Exception e){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs excetion:" + e);
    }
    
    /**
     * 環繞通知需要攜帶 ProceedingJoinPoint 類型的參數. 
     * 環繞通知類似於動態代理的全過程: ProceedingJoinPoint 類型的參數可以決定是否執行目標方法.
     * 且環繞通知必須有返回值, 返回值即為目標方法的返回值
     */
    /*
    @Around("execution(public int com.hp.spring.aop.annotation.ArithmeticCalculator.*(..))")
    public Object aroundMethod(ProceedingJoinPoint pjd){
        
        Object result = null;
        String methodName = pjd.getSignature().getName();
        
        try {
            //前置通知
            System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
            //執行目標方法
            result = pjd.proceed();
            //返回通知
            System.out.println("The method " + methodName + " ends with " + result);
        } catch (Throwable e) {
            //異常通知
            System.out.println("The method " + methodName + " occurs exception:" + e);
            throw new RuntimeException(e);
        }
        //后置通知
        System.out.println("The method " + methodName + " ends");
        
        return result;
    }
    */
}
  • 切面二
package com.hp.spring.aop.annotation;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(1)
@Aspect
@Component
public class VlidationAspect {
    @Before("com.hp.spring.aop.annotation.LoggingAspect.declareJointPointExpression()")
    public void validateArgs(JoinPoint joinPoint){
        System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs()));
    }
    
}

  • 測試類
package com.hp.spring.aop.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    
    public static void main(String[] args) {
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-aspectj.xml");
        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
        
        System.out.println(arithmeticCalculator.getClass().getName());
        
        int result = arithmeticCalculator.add(1, 2);
        System.out.println("result:" + result);
        
        result = arithmeticCalculator.div(1000, 10);
        System.out.println("result:" + result);
    }
    
}

打印出:
com.sun.proxy.$Proxy12
-->validate:[1, 2]
The method add begins with [1, 2]
The method add ends
The method add ends with 3
result:3
-->validate:[1000, 10]
The method div begins with [1000, 10]
The method div ends
The method div ends with 100
result:100

打印的日志中,可以看出,跟動態代理一樣,可以定義各類通知。

整個系列項目代碼: http://git.oschina.net/nmc5/spring


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM