Spring+SpringMVC+maven使用@aspectJ添加切面

    我也是根據網上的信息自己做的,僅供參考。

    明確一點:使用的是@Aspect註解方式,項目是maven項目。

    使用@Aspect註解方式,需要修改的地方主要有:

    1、pom文件,增加:

    

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.4</version>
    </dependency>

    這裏需要注意一個問題。我一開始這裏使用的版本是1.5.4,但是在啓動的時候,會遇到一個報錯,

    0 can't find referenced pointcut,這個問題讓我很費解,後來經過網上查資料,說是環境的問題。

    一般情況下:

    wKioL1h4qInB24fWAAAaIrreKDk728.png-wh_50

    我的jdk版本是1.7的,所以換成1.7.4ok了。

    2、SpringMVC配置,我這兒是默認的名字,叫springMVC-servlet.xml

    這個配置文件裏需要加上

    

    <!-- 掃描aop,該類對應自己定義的註解類-->
    <context:component-scan base-package="com.tarena.vote.aspect" />
    <!--啓動對AspectJ註解的支持,proxy-target-class等於true是強制使用cglib代理-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    3、定義註解類。

    說明:我這個是實驗性質的,所以並沒有什麼具體 的業務代碼。裏面可以添加你需要的業務,例如:日誌記錄,事務控制等等。

package com.tarena.vote.aspect;

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 org.springframework.stereotype.Component;

/**
 * <p>
 *    Description: 註解邏輯類(測試,無實際意義)
 * </p>
 *
 * @author fcl
 * @date 2017年1月11日
 * @version v_1.0
 */
@Aspect
@Component
public class AspectAdvice {
    /**
     * Pointcut
     * 定義Pointcut,Pointcut名稱爲aspectjMethod,必須無參,無返回值
     * 只是一個標識,並不進行調用
     * 切點我這兒定義的是爲controller包下的所有類,所有方法都加,
     * 你可以指定具體的類或具體的方法
     */
    @Pointcut("execution(* com.tarena.vote.web.controller.*.*(..))")
    //@Pointcut("@annotation(com.tarena.vote.aspect.AspectAdvice)")
    private void aspectJMethod(){};

    @Before("aspectJMethod()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("----dobefore()開始----");
        System.out.println("執行業務邏輯前做一些工作");
        System.out.println("通過jointPoint獲得所需內容");
        System.out.println("----dobefore()結束----");
    }
    @Around("aspectJMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{

        System.out.println("----doAround()開始----");
        System.out.println("此處可做一些類似before的工作");
        //核心邏輯
        Object retval=pjp.proceed();
        System.out.println("此處可做一些類似after的工作");
        System.out.println("----doAround()結束----");
        return retval;
    }
    @After(value="aspectJMethod()")
    public void doAfter(JoinPoint joinPoint){
        System.out.println("----doAfter()開始----");
        System.out.println("執行核心邏輯之後,所做工作");
        System.out.println("通過jointPoint獲得所需內容");
        System.out.println("----doAfter()結束----");
    }

    @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("----AfterReturning()結束----");

    }

    @AfterThrowing(value="aspectJMethod()", throwing="e")
    public void doThrowing(JoinPoint joinPoint,Exception e){
        System.out.println("-----doThrowing()開始-----");
        System.out.println(" 錯誤信息:"+e.getMessage());
        System.out.println(" 此處意在執行核心業務邏輯出錯時,捕獲異常,並可做一些日誌記錄操作等等");
        System.out.println(" 可通過joinPoint來獲取所需要的內容");
        System.out.println("-----End of doThrowing()------");
    }
}
/**
 * 自定義註解。
 * 這個自定義註解我也不知是用來幹啥的,我感覺可能是用來描述的吧,
 * 感覺項目中並沒有地方需要用到,但是看網上的例子都有加
 * 如果大神看見了,幫我解答一下。
 */
package com.tarena.vote.aspect;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 *自定義註解 攔截Controller
 */

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public  @interface SystemLog {
    String operationType()  default "";   //操作類型
    String methods()  default "";  //新增用戶
    String description()  default "";  //
}

至此,需要做的工作已經完成了。運行項目的結果如下:

wKiom1h4qv6AHp4WAAB8J0eDzz0820.png-wh_50

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章