Spring MVC AOP處理事務

只需兩步:java

一、引入 支持aop xml頭,添加本身類:web

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"


...
...

     <!-- 啓動AspectJ支持 -->  
    <aop:aspectj-autoproxy />  
	<bean id="aspectJAdvice" class="com.util.aop.AspectAdvice"></bean>

二、添加本身的 aop類,用註解來 切面 ,攔截。spring

package com.util.aop;
 
import java.sql.SQLException;
import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.util.webapi.DBUtils;
/***
 * spring AOP 切面   自動控制事務
 * 
 * @author 姚林濤
 * 2017-9-22
 */
@Aspect
public class AspectAdvice {
    /**
     * Pointcut 定義Pointcut,Pointcut的名稱爲aspectjMethod(),此方法沒有返回值和參數
     * 該方法就是一個標識,不進行調用
     */       //execution(* *(..))
    @Pointcut("execution(* com.service..*.*(..))")
    private void aspectjMethod() {
    };
 
    // 自定義After
    //@After("execution(* add*(..))")
    public void doSava(JoinPoint joinPoint) {
        System.out.println("-----------@After這是日誌:新增用戶成功---" + new Date());
    }
 
    /**
     * Before 在覈心業務執行前執行,不能阻止核心業務的調用。
     * 
     * @param joinPoint
     * @throws SQLException 
     */
    @Before("aspectjMethod()")
    public void doBefore(JoinPoint joinPoint) throws SQLException {
    	DBUtils.openConn();
        System.out.println("-----@Before-----");
//        System.out.println(" 此處意在執行核心業務邏輯前,作一些安全性的判斷等等");
//        System.out.println(" 可經過joinPoint來獲取所須要的內容");
//        System.out.println("-----End of doBefore()------");
    }
 
    /**
     * Around 手動控制調用核心業務邏輯,以及調用前和調用後的處理,
     * 
     * 注意:當核心業務拋異常後,當即退出,轉向AfterAdvice 執行完AfterAdvice,再轉到ThrowingAdvice
     * 
     * @param pjp
     * @return
     * @throws Throwable
     */
    //@Around(value = "aspectjMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("-----doingBefor-----");
        // 調用核心邏輯
        Object retVal = pjp.proceed();
        System.out.println("-----doingAfter-----");
        return retVal;
    }
 
    /**
     * After 核心業務邏輯退出後(包括正常執行結束和異常退出),執行此Advice
     * 
     * @param joinPoint
     * @throws SQLException 
     */
    @After(value = "aspectjMethod()")
    public void doAfter(JoinPoint joinPoint) throws SQLException {
    	DBUtils.commit();
    	DBUtils.closeConnection();
        System.out.println("-----@After-----");
//        System.out.println(" 此處意在執行核心業務邏輯以後,作一些日誌記錄操做等等");
//        System.out.println(" 可經過joinPoint來獲取所須要的內容");
//        System.out.println("-----End of doAfter()------");
    }
 
    /**
     * AfterReturning 核心業務邏輯調用正常退出後,無論是否有返回值,正常退出後,均執行此Advice
     * 
     * @param joinPoint
     */
    //@AfterReturning(value = "aspectjMethod()", returning = "retVal")
    public void doReturn(JoinPoint joinPoint, String retVal) {
        System.out.println("-----@AfterReturning-----");
//        System.out.println("Return Value: " + retVal);
//        System.out.println(" 此處能夠對返回值作進一步處理");
//        System.out.println(" 可經過joinPoint來獲取所須要的內容");
//        System.out.println("-----End of doReturn()------");
    }
 
    /**
     * 核心業務邏輯調用異常退出後,執行此Advice,處理錯誤信息
     * 
     * 注意:執行順序在Around Advice以後
     * 
     * @param joinPoint
     * @param ex
     * @throws SQLException 
     */
    @AfterThrowing(value = "aspectjMethod()", throwing = "ex")
    public void doThrowing(JoinPoint joinPoint, Exception ex) throws SQLException {
    	DBUtils.rollback();
        System.out.println("-----AOP Print: Exception :-----");
        ex.printStackTrace();
        //執行後  交給 spring 異常處理器 處理返回參數  -MyExceptionHandler
        
    }
}

跑起來  走兩步 便可。(注意Jar包 pom.xml配置 也說一下吧)sql

<dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.1</version>
    </dependency>

這是我引入的包,檢查下本身是否引入。api

相關文章
相關標籤/搜索