欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Spring 中使用@Aspect 控制自定義注解的操作

 更新時(shí)間:2021年01月20日 14:26:46   作者:jmdonghao  
這篇文章主要介紹了在Spring 中使用@Aspect 控制自定義注解的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

Spring 中使用@Aspect 控制自定義注解

看這篇介紹@Aspect

1.定義系統(tǒng)日志注解類

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
String value() default "";
}

2.定義切面處理類

package com.kxs.common.aspect;
import com.google.gson.Gson;
import com.kxs.common.annotation.SysLog;
import com.kxs.common.utils.HttpContextUtils;
import com.kxs.common.utils.IPUtils;
import com.kxs.modules.sys.entity.SysLogEntity;
import com.kxs.modules.sys.entity.SysUserEntity;
import com.kxs.modules.sys.service.SysLogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
/**
 * 系統(tǒng)日志,切面處理類
 * 
 * @author 
 * @email 
 * @date 
 */
@Aspect
@Component
public class SysLogAspect {
 @Autowired
 private SysLogService sysLogService;
 @Pointcut("@annotation(com.kxs.common.annotation.SysLog)")//指向自定義注解路徑
 public void logPointCut() { 
 }
 /**
  * 切面記錄系統(tǒng)日志
  * @param point
  * @return
  * @throws Throwable
  */
 @Around("logPointCut()")//
 public Object around(ProceedingJoinPoint point) throws Throwable {
  long beginTime = System.currentTimeMillis();
  //執(zhí)行方法
  Object result = point.proceed();
  //執(zhí)行時(shí)長(zhǎng)(毫秒)
  long time = System.currentTimeMillis() - beginTime;
  //保存日志
  saveSysLog(point, time);
  return result;
 }
//保存日志
 private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLogEntity sysLog = new SysLogEntity();
  SysLog syslog = method.getAnnotation(SysLog.class);
  if(syslog != null){
   //注解上的描述
   sysLog.setOperation(syslog.value());
  }
  //請(qǐng)求的方法名
  String className = joinPoint.getTarget().getClass().getName();
  String methodName = signature.getName();
  sysLog.setMethod(className + "." + methodName + "()");
  //請(qǐng)求的參數(shù)
  Object[] args = joinPoint.getArgs();
  try{
   String params = new Gson().toJson(args[0]);
   sysLog.setParams(params);
  }catch (Exception e){
  }
  //獲取request
  HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
  //設(shè)置IP地址
  sysLog.setIp(IPUtils.getIpAddr(request));
  //用戶名
  String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
  sysLog.setUsername(username);
  sysLog.setTime(time);
  sysLog.setCreateDate(new Date());
  //保存系統(tǒng)日志
  sysLogService.save(sysLog);
 }
}

補(bǔ)充:為什么添加了@Aspect 還要加@Component

官方文檔中有寫:

You may register aspect classes as regular beans in your Spring XML configuration, or autodetect them through classpath scanning - just like any other Spring-managed bean. However, note that the @Aspect annotation is not sufficient for autodetection in the classpath: For that purpose, you need to add a separate @Component annotation (or alternatively a custom stereotype annotation that qualifies, as per the rules of Spring's component scanner).

翻譯:

您可以在Spring XML配置中注冊(cè)aspect類,或者通過(guò)類路徑掃描自動(dòng)檢測(cè)它們,就像任何其他Spring管理bean一樣。但是,請(qǐng)注意,@aspect注釋對(duì)于在類路徑中自動(dòng)檢測(cè)是不夠的:為了達(dá)到這個(gè)目的,您需要添加一個(gè)單獨(dú)的@component注解(或者根據(jù)Spring的組件掃描器的規(guī)則來(lái)定義一個(gè)定制的原型注解)。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • JFormDesigner(IDEA)下載方法

    JFormDesigner(IDEA)下載方法

    JFormDesigner是一種Java Swing GUI設(shè)計(jì)工具,可快速創(chuàng)建用戶界面,支持多種布局管理器,如GridBagLayout、SpringLayout等,本文給大家介紹JFormDesigner(IDEA)下載方法,感興趣的朋友跟隨小編一起看看吧
    2023-12-12
  • SpringMVC 中配置 Swagger 插件的教程(分享)

    SpringMVC 中配置 Swagger 插件的教程(分享)

    下面小編就為大家分享一篇SpringMVC 中配置 Swagger 插件的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • Springboot @Validated和@Valid的區(qū)別及使用詳解

    Springboot @Validated和@Valid的區(qū)別及使用詳解

    這篇文章主要介紹了Springboot @Validated和@Valid的區(qū)別及使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Log4j定時(shí)打印日志及添加模塊名配置的Java代碼實(shí)例

    Log4j定時(shí)打印日志及添加模塊名配置的Java代碼實(shí)例

    這篇文章主要介紹了Log4j定時(shí)打印日志及添加模塊名配置的Java代碼實(shí)例,Log4j是Apache的一個(gè)開源Java日志項(xiàng)目,需要的朋友可以參考下
    2016-01-01
  • 關(guān)于easyExcel中讀取Excel表頭的實(shí)例說(shuō)明

    關(guān)于easyExcel中讀取Excel表頭的實(shí)例說(shuō)明

    EasyExcel是阿里巴巴開源的一個(gè)excel處理框架,以使用簡(jiǎn)單、節(jié)省內(nèi)存著稱,下面這篇文章主要給大家介紹了關(guān)于easyExcel中讀取Excel表頭的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Java通過(guò)反射查看類的信息示例

    Java通過(guò)反射查看類的信息示例

    這篇文章主要介紹了Java通過(guò)反射查看類的信息,結(jié)合實(shí)例形式詳細(xì)分析了java基于反射獲取類信息的相關(guān)原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-07-07
  • Java import static及import原理區(qū)別解析

    Java import static及import原理區(qū)別解析

    這篇文章主要介紹了Java import static及import原理區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java Scala泛型(泛型方法,泛型類,泛型特質(zhì),上下界,協(xié)變、逆變、非變)

    Java Scala泛型(泛型方法,泛型類,泛型特質(zhì),上下界,協(xié)變、逆變、非變)

    泛型的意思是泛指某種具體的數(shù)據(jù)類型, 在Scala中, 泛型用[數(shù)據(jù)類型]表示. 在實(shí)際開發(fā)中, 泛型一般是結(jié)合數(shù)組或者集合來(lái)使用的,這篇文章主要介紹了Scala泛型(泛型方法,泛型類,泛型特質(zhì),上下界,協(xié)變、逆變、非變),需要的朋友可以參考下
    2023-04-04
  • java中全排列的生成算法匯總

    java中全排列的生成算法匯總

    本文給大家匯總介紹了常見(jiàn)的6種全排列的生成算法,包括字典序法、遞增進(jìn)位數(shù)制法、遞減進(jìn)位數(shù)制法、鄰位交換法、遞歸類算法、元素增值法,有需要的小伙伴可以參考下
    2015-07-07
  • 詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié)

    詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié)

    本篇文章主要介紹了詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04

最新評(píng)論