spring boot使用自定義注解做AOP的案例代碼
1、創(chuàng)建一個自定注解,接收一個傳值type
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EchoStatus {
String type();
}2、創(chuàng)建一個切面類,綁定一些切面方法,比如before,after…
@Aspect
@Component
@Slf4j
public class EchoStatusAspect {
@Pointcut("@annotation(com.gbs.mgt.annotation.EchoStatus)")
public void customPointcut() {
}
@Before("customPointcut()")
public void beforeAdvice(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
System.out.println("Before method execution: " + joinPoint.getSignature().getName()+"入?yún)ⅲ?+ Arrays.asList(args));
}
@After(value = "customPointcut()")
public void afterAdvice(JoinPoint joinPoint) {
System.out.println("After method execution: " + joinPoint.getSignature().getName());
}
@AfterReturning(value = "customPointcut()", returning = "result")
public void afterReturningAdvice(JoinPoint joinPoint, Object result) {
System.out.println("After method execution: " + joinPoint.getSignature().getName()+"結(jié)果:"+result);
}
}@EchoStatus (type = "無所謂")
public String index(){
return "hello word";
}到此這篇關(guān)于spring boot使用自定義注解做AOP的文章就介紹到這了,更多相關(guān)spring boot使用自定義注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 實用注解篇之@Qualifier 深度解析及實戰(zhàn)案例
在Spring框架中,@Qualifier是一個常見的注解,主要用于解決依賴注入(DI)時的歧義性,本文給大家介紹Java 實用注解篇之@Qualifier 深度解析及實戰(zhàn)案例,感興趣的朋友一起看看吧2025-06-06
SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù)
本文主要介紹了SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Spring-Cloud-Function-Spel?漏洞環(huán)境搭建
這篇文章主要介紹了Spring-Cloud-Function-Spel?漏洞復(fù)現(xiàn)及搭建方法,搭建方法也很簡單,首先需要安裝maven jdk,具體安裝過程跟隨小編一起看看吧2022-03-03
No ‘Access-Control-Allow-Origin‘ header is&nb
這篇文章主要介紹了No ‘Access-Control-Allow-Origin‘ header is present跨域及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
基于多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊IP的選擇問題
這篇文章主要介紹了基于多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊IP的選擇問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
springMVC配置環(huán)境實現(xiàn)文件上傳和下載
這篇文章主要為大家詳細介紹了springMVC配置環(huán)境實現(xiàn)文件上傳和下載的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05

