springboot aop使用方式

2022-09-20 10:30:08 字數 2993 閱讀 3270

定義切面類

切面類進行增強的兩種寫法

切點匹配註解和規則

引入相應的aop包

            

org.springframework.bootgroupid>

spring-boot-starter-aopartifactid>

dependency>

定義切面類(類名自定義)

import org.aspectj.lang.annotation.aspect;

import org.springframework.stereotype.component;

​@aspect

@component

public class databaseresourcetoggle

@pointcut :用來定義乙個切面(切入點),即所關注的某件事情的入口。切入點決定了連線點關注的內容,使得我們可以控制通知什麼時候執行。

先說明兩個萬用字元:*、..

*:主要用於匹配單個單詞,或者是以某個詞為字首或字尾的單詞

..:表示0個或多個項。主要用於類全路徑或者引數,用於類全路徑中表示匹配當前包及其子包,如果用於引數中,則表示匹配0個或多個參

1、匹配使用自定義註解的方法:

@pointcut("@annotation()")

@pointcut("@within()")

@pointcut("@args()")

@pointcut("@target()")

2、匹配物件下的方法:

@pointcut("this()")

@pointcut("bean()")

@pointcut("target()")

3、匹配方法和引數:

@pointcut("within()")

@pointcut("args()")

@pointcut("execution()")

@pointcut("@annotation(com.gzt.annotation.databaseresourcedefine)"):匹配方法上有databaseresourcedefine註解的方法

@pointcut("@within(com.gzt.annotation.databaseresourcedefine)"):匹配有databaseresourcedefine註解標記的類裡面的方法(最常用)

@pointcut("@args(com.gzt.annotation.databaseresourcedefine)"):匹配傳入的引數有databaseresourcedefine註解標記的方法

@pointcut("@target(com.gzt.annotation.databaseresourcedefine)"):匹配有databaseresourcedefine註解標記的類及其子類裡面的方法

@pointcut("this(com.gzt.annotation.databaseresourcedefine)"):匹配aop物件的目標物件為指定型別的方法

@pointcut("target(com.gzt.annotation.databaseresourcedefine)"):匹配實現databaseresourcedefine介面的目標物件裡面的方法

@pointcut("bean(databaseresourcedefine)"):匹配指定的bean

@pointcut("within(com.gzt.annotation.databaseresourcedefine)"):匹配指定包下的方法

@pointcut("args(long,..)"):匹配任何以long引數開頭的方法(一般搭配其他匹配條件使用)

@pointcut("execution(* com.gzt.service.impltestimpl.*(..))"):匹配某個方法(最常用)

@within、execution兩個一般最常用,使用@within+自定義註解,可以很好的對某乙個類下面的方法進行增強,execution用來匹配單個的

@before(前置通知): 在方法開始執行前執行

@after(後置通知): 在方法執行後執行

@afterreturning(返回後通知): 在方法返回後執行

@afterthrowing(異常通知): 在丟擲異常時執行

@around(環繞通知): 在方法執行前和執行後都會執行

例子:

public class databaseresourcetoggle ​​

@before(value = "pointcut()")

public void before1(joinpoint joinpoint)

object target = joinpoint.gettarget();

system.out.println(target.getclass().gettypename());

object target1 = joinpoint.gettarget();

databaseresourcedefine annotation = target1.getclass().getannotation(databaseresourcedefine.class);

string value = annotation.value();

system.out.println("value:"+value);

databaseadapt.flag = value;

logger.info(value);

}//使用前置通知或者是環繞通知

//@before("@annotation(databaseresourcedefine) || @within(databaseresourcedefine)")

//public void before(joinpoint joinpoint, databaseresourcedefine databaseresourcedefine)

}

SpringBoot AOP整合和基本使用

aop aspect oriented programming 即面向切面程式設計。引入springboot的web模組 org.springframework.boot spring boot starter web 引入aop依賴 org.springframework.boot spring ...

SpringBoot AOP程式設計

aop org.springframework.boot groupid spring boot starter aop artifactid dependency spring 的aop預設是使用jdk spring aop預設是使用jdk動態 即基於介面的 可以通過proxy target cl...

SpringBoot AOP 初步理解

剛開始接觸spring的時候不可避免的會提到aop,網上一查,給你一句話就叫切面程式設計,個人在這裡提出下我自己的一些理解以及乙個例子 記得小時候在夏天的時候,有一次說風來吧風就來了,雨來吧雨就來了,而不需要我們去拿乙個吹風機製造風,或者製造雨。那麼aop就如同我們小時候想像的咒語一樣,說來就來,只...