Springboot自定義注解&傳參&簡單應用方式
更新時間:2024年10月25日 09:24:23 作者:寫....寫個大西瓜
SpringBoot框架中,通過自定義注解結合AOP可以實現功能如日志記錄與耗時統(tǒng)計,首先創(chuàng)建LogController和TimeConsuming注解,并為LogController定義參數,然后,在目標方法上應用這些注解,最后,使用AspectJ的AOP功能,通過切點表達式定位這些注解
Springboot自定義注解&傳參&簡單應用
1、目錄結構
1.1 annotation為自定義注解位置
2、自定義注解
2.1 自定義兩個注解LogController、TimeConsuming用來記錄日志和統(tǒng)計方法耗時,其中LogController有三個參數
@Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface LogController { // 具體操作 String description(); // 日志級別 int logLevel() default LogLevelConstant.INFO; // 日志進程/方法名 String method(); }
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface TimeConsuming { }
3、注解使用
3.1 在需要記錄日志和統(tǒng)計方法耗時的方法上使用注解
@PostMapping("/createUser") @LogController(description = "創(chuàng)建用戶", method = "/createUser") @TimeConsuming public ResponseEntity<ResponseResultVO> createUser(@Valid @RequestBody SysUsersVo sysUsersVo) { log.info("UserManager = start create user [{}] pwd [{}]", sysUsersVo.getUserName(), sysUsersVo.getUserPwd()); return ResponseEntity.ok(usersService.createUser(sysUsersVo).orElse(ResponseResultVO.builder() .code(ErrorCodeConstant.SYSTEM_ERROR) .msg(ErrorMsgConstant.SYSTEM_ERROR) .build())); }
4、使用Aspect實現功能
4.1 通過Aop以注解作為切點切入方法,實現業(yè)務功能
@Pointcut("@annotation(com.pet.annotation.LogController)") public void annotationPoint() { } @Pointcut("@annotation(com.pet.annotation.TimeConsuming)") public void methodTimePoint() { }
4.2 取注解中參數
比如下面方法實現記錄日志功能
@Before(value = "annotationPoint() && @annotation(logController)", argNames = "joinPoint, logController") public void beforeController(JoinPoint joinPoint, LogController logController) { HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); HttpSession session = request.getSession(); SysUsers user = (SysUsers) session.getAttribute(HttpConstant.SESSION_USER); String realMethodName = joinPoint.getSignature().getName(); log.info("Aspect = [{}] ,user [{}] , method [{}] , logLevel [{}] , do [{}] , realMethod [{}]", new Date(), user == null ? "system" : user.getUserName(), logController.method(), logController.logLevel(), logController.description(), realMethodName); // 異步處理日志 publisher.publishEvent(new LogToDbEvent( LogToDbEventEntity.builder() .date(new Date()) .userName(user == null ? "system" : user.getUserName()) .method(logController.method()) .logLevel(logController.logLevel()) .description(logController.description()) .realMethod(realMethodName) .build())); }
統(tǒng)計方法耗時方法
@Around(value = "methodTimePoint()") public Object apiTimeConsuming(ProceedingJoinPoint pjp) throws Throwable { long begin = System.currentTimeMillis(); String method = pjp.getSignature().getName(); String className = pjp.getTarget().getClass().getName(); Object ret = pjp.proceed(); log.info("Aspect = [{}] ,class [{}] , method [{}] , time consuming[{}]", new Date(), className, method, System.currentTimeMillis() - begin); return ret; }
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot2底層注解@Configuration配置類詳解
這篇文章主要為大家介紹了SpringBoot2底層注解@Configuration配置類詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05SpringBoot?+?Vue?+?ElementUI?實現?el-table?分頁功能(詳細步驟)
本文詳細介紹了使用SpringBoot和Vue.js結合ElementUI實現分頁功能的數據表格,從后端分頁邏輯到前端展示和狀態(tài)管理,全面解析如何高效處理大量數據,提升用戶體驗與系統(tǒng)性能,感興趣的朋友跟隨小編一起看看吧2024-09-09