ホームページ  >  記事  >  Java  >  springboot での aop の使用の概要 (コード例)

springboot での aop の使用の概要 (コード例)

不言
不言転載
2018-10-15 15:51:152308ブラウズ

この記事では、springboot での aop の使用法 (コード例) を紹介します。必要な方は参考にしていただければ幸いです。

ステップ 1: 依存関係の追加

        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-aop</artifactId>  
        </dependency>

#ステップ 2: アスペクト クラスの定義

#
package com.example.demo.aop;
import java.lang.reflect.Method;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import static com.sun.xml.internal.ws.dump.LoggingDumpTube.Position.Before;
@Component
@Aspect // 将一个java类定义为切面类
@Order(-1)//如果有多个aop,这里可以定义优先级,越小级别越高
public class LogDemo {
    private static final Logger LOG = LoggerFactory.getLogger(LogDemo.class);

    @Pointcut("execution(* com.example.demo.test.TestController.test(..))")//两个..代表所有子目录,最后括号里的两个..代表所有参数
    public void logPointCut() {
    }
    @Before("logPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("before");

    }

    @After(value = "logPointCut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("after");
    }


    @AfterReturning(returning = "ret", pointcut = "logPointCut()")// returning的值和doAfterReturning的参数名一致
    public void doAfterReturning(Object ret) throws Throwable {
        System.out.println("AfterReturning");
    }

    @Around("logPointCut()")
    public void doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around1");
        Object ob = pjp.proceed();//环绕通知的进程方法不能省略,否则可能导致无法执行
        System.out.println("around2");
    }
}

注:

同じアスペクト クラスで 2 つの @Before が定義されている場合、

#@ の場合、2 つの @Before の実行順序を決定できません。戻り値があるかどうかに関係なく、メソッド内で pjp.proceed() を呼び出す必要があります。そうしないと、コントローラー内のインターフェイスが実行されず、その結果 @Before も実行されません。

テストでトリガーされるコントローラーは次のとおりです:

package com.example.demo.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class TestController {

    @RequestMapping(value = "test",method = RequestMethod.GET)
    @ResponseBody
    public String test(String name){
        System.out.println("============method");
        return name;
    }
}
設定が完了しました。効果を確認します。出力は次のとおりです。

around1
before============method
around2
after
AfterReturning

ご覧のとおり、アスペクト メソッドの実行は次のとおりです。

around-->before-->method- ->around-->after-->AfterReturning

@AfterThrowing が設定されている場合、例外が発生すると、実行は次のようになります:

around-->before- ->メソッド-->周囲-->後-->投げた後

以上がspringboot での aop の使用の概要 (コード例)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はcnblogs.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。