天天看點

Spring基于@Aspect的AOP配置

Spring AOP面向切面程式設計,可以用來配置事務、做日志、權限驗證、在使用者請求時做一些處理等等。用@Aspect做一個切面,就可以直接實作。

1、包結構

Spring基于@Aspect的AOP配置

 2、切面類

  execution解釋:

    1)execution(), 表達式主體

    2)第一個*表示傳回類型,*表示所有類型

    3)包名,後面兩個..表示目前包和目前包所有子包

    4)第二個*表示所有類名,*表示所有類

    5)*(..):最後這個星号表示方法名,*号表示所有的方法,後面括弧裡面表示方法的參數,兩個句點表示任何參數。

Spring基于@Aspect的AOP配置
Spring基于@Aspect的AOP配置
package com.test;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author zyydd
 * @date 2019/12/25 19:53
 */
@Aspect
@Service
public class BizLogAop {
    private static final Logger logger = LoggerFactory.getLogger(BizLogAop.class);


    @Around("execution(* com.test.bizA..*.*(..)) || " +
            "execution(* com.test.bizB.TestServiceB1.*(..)) || " +
            "execution(* com.test.bizC.TestServiceC.test1(..))")
    public Object aroundBiz(ProceedingJoinPoint pjp) throws Throwable {
        String methodFullName = "";
        try {
            methodFullName = String.format("%s.%s", pjp.getSignature().getDeclaringType().getName(), pjp.getSignature().getName());
            logger.info("methodFullName={} 開始調用,參數={}", methodFullName, JSON.toJSON(pjp.getArgs()));
        } catch (Exception ex) {
            logger.error("BizLogAop類異常", ex);
        }
        //調用實際的方法,如果沒有異常直接傳回結果
        Object result = pjp.proceed(pjp.getArgs());
        try {
            logger.info("methodFullName={} 調用完成,傳回值={}", methodFullName, JSON.toJSON(result));
        } catch (Exception ex) {
            logger.error("BizLogAop類異常", ex);
        }

        return result;
    }

}      

BizLogAop

3、TestService類舉例

很簡單,通過注解,注冊一個service,然後傳回一個字元串。

Spring基于@Aspect的AOP配置
Spring基于@Aspect的AOP配置
package com.test.bizA;

import org.springframework.stereotype.Service;

/**
 * @author zyydd
 * @date 2019/12/26 9:35
 */
@Service
public class TestServiceA {
    public String test1(){
        return "TestServiceA1";
    }
    public String test2(){
        return "TestServiceA2";
    }
}      

TestServiceA

4、單元測試類

Spring基于@Aspect的AOP配置
Spring基于@Aspect的AOP配置
package com.test.bizA.bizA_a;

import com.**.web.Application;
import com.test.bizA.TestServiceA;
import com.test.bizB.TestServiceB1;
import com.test.bizB.TestServiceB2;
import com.test.bizC.TestServiceC;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author laizy
 * @date 2019/12/26 9:56
 */
@SpringBootTest(classes = Application.class)
@RunWith(SpringRunner.class)
public class ServiceTest {

    public static final Logger logger = LoggerFactory.getLogger(ServiceTest.class);

    @Autowired
    private TestServiceA testServiceA;
    @Autowired
    private TestServiceAa testServiceAa;
    @Autowired
    private TestServiceB1 testServiceB1;
    @Autowired
    private TestServiceB2 testServiceB2;
    @Autowired
    private TestServiceC testServiceC;

    @Test
    public void testAll() {
        logger.info(testServiceA.test1());
        logger.info(testServiceA.test2());
        logger.info(testServiceAa.test());
    }

    @Test
    public void testOneClass() {
        logger.info(testServiceB1.test());
        logger.info(testServiceB2.test());
    }

    @Test
    public void testOneMethod() {
        logger.info(testServiceC.test1());
        logger.info(testServiceC.test2());
    }
}      

ServiceTest

 第一個測試對應execution中第一個表達式:execution(* com.test.bizA..*.*(..))  即bizA下所有包,所有子包,所有方法都在作用範圍内,執行結果:

Spring基于@Aspect的AOP配置

第二個測試對應execution中第二個表達式:execution(* com.test.bizB.TestServiceB1.*(..))  即bizB下TestServiceB1類的所有方法都在作用範圍内,執行結果:

第三個測試對應execution中第三個表達式:execution(* com.test.bizC.TestServiceC.test1(..))  即bizC下TestServiceC類的test1方法在作用範圍内,執行結果:

Spring基于@Aspect的AOP配置