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

Spring AOP實(shí)現(xiàn)復(fù)雜的日志記錄操作(自定義注解)

 更新時(shí)間:2021年09月27日 10:11:09   作者:小草mlc  
Spring AOP實(shí)現(xiàn)復(fù)雜的日志記錄操作(自定義注解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Spring AOP復(fù)雜的日志記錄(自定義注解)

做項(xiàng)目中,業(yè)務(wù)邏輯要求只要對(duì)數(shù)據(jù)庫(kù)數(shù)據(jù)進(jìn)行改動(dòng)的都需要記錄日志(增刪改),記錄的內(nèi)容有操作者、操作的表名及表名稱、具體的操作,以及操作對(duì)應(yīng)的數(shù)據(jù)。

首先想到的就是Spring 的AOP功能??墒墙?jīng)過(guò)一番了解過(guò)后,發(fā)現(xiàn)一般的日志記錄,只能記錄一些簡(jiǎn)單的操作,例如表名、表名稱等記錄不到。

于是想到了自定義注解的方法,把想要記錄的內(nèi)容放在注解中,通過(guò)切入點(diǎn)來(lái)獲取注解參數(shù),就能獲取自己想要的數(shù)據(jù),記錄數(shù)據(jù)庫(kù)中。順著這個(gè)思路,在網(wǎng)上查找了一些相關(guān)資料,最終實(shí)現(xiàn)功能。話不多說(shuō),以下就是實(shí)現(xiàn)的思路及代碼:

第一步

在代碼中添加自定義注解,并且定義兩個(gè)屬性,一個(gè)是日志的描述(description),還有個(gè)是操作表類型(tableType),屬性參數(shù)可按需求改變。代碼如下:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;  
 /**
 * ClassName: SystemServiceLog <br/>
 * Function: AOP日志記錄,自定義注解 <br/>
 * date: 2016年6月7日 上午9:29:01 <br/>
 * @author lcma
 * @version 
 * @since JDK 1.7
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})    
@Retention(RetentionPolicy.RUNTIME)    
@Documented  
public @interface SystemServiceLog {
/**
* 日志描述
*/
String description()  default ""; 
 
/**
* 操作表類型
*/
int tableType() default 0; 
}

第二步

定義切面類,獲取切面參數(shù),保存數(shù)據(jù)庫(kù)具體代碼如下:

import java.lang.reflect.Method;
import java.util.Date; 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; 
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; 
 
import com.iflytek.zhbs.common.annotation.SystemServiceLog;
import com.iflytek.zhbs.common.util.JacksonUtil;
import com.iflytek.zhbs.common.util.WebUtils;
import com.iflytek.zhbs.dao.BaseDaoI;
import com.iflytek.zhbs.domain.CmsAdmin;
import com.iflytek.zhbs.domain.CmsOperationLog; 
 
@Aspect
@Component
@SuppressWarnings("rawtypes")
public class SystemLogAspect {
 
@Resource
private BaseDaoI<CmsOperationLog> logDao;
 
    /**
     * 日志記錄
     */
    private static final Logger LOGGER = Logger.getLogger(SystemLogAspect.class);
 
     /**
      * Service層切點(diǎn)
      */
     @Pointcut("@annotation(com.iflytek.zhbs.common.annotation.SystemServiceLog)")    
     public void serviceAspect() {         
     }
     
     /**
     * doServiceLog:獲取注解參數(shù),記錄日志. <br/>
     * @author lcma
     * @param joinPoint 切入點(diǎn)參數(shù)
     * @since JDK 1.7
     */
    @After("serviceAspect()") 
     public  void doServiceLog(JoinPoint joinPoint) {
    LOGGER.info("日志記錄");
         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
         //獲取管理員用戶信息
    CmsAdmin admin = WebUtils.getAdminInfo(request);
          try {
             //數(shù)據(jù)庫(kù)日志
             CmsOperationLog log = new CmsOperationLog();
             log.setOperationType(getServiceMthodTableType(joinPoint));
             //獲取日志描述信息
             String content = getServiceMthodDescription(joinPoint);
             log.setContent(admin.getRealName() + content);
             log.setRemarks(getServiceMthodParams(joinPoint));
             log.setAdmin(admin);
             log.setCreateTime(new Date());
             logDao.save(log);
         }  catch (Exception e) {  
             LOGGER.error("異常信息:{}", e);
         }    
     }     
     
    /**
     * getServiceMthodDescription:獲取注解中對(duì)方法的描述信息 用于service層注解  . <br/>
     * @author lcma
     * @param joinPoint 切點(diǎn) 
     * @return 方法描述
     * @throws Exception 
     * @since JDK 1.7
     */
    private String getServiceMthodDescription(JoinPoint joinPoint)
               throws Exception {
          String targetName = joinPoint.getTarget().getClass().getName();
          String methodName = joinPoint.getSignature().getName();
          Object[] arguments = joinPoint.getArgs();
          Class targetClass = Class.forName(targetName);
          Method[] methods = targetClass.getMethods();
          String description = "";
           for(Method method : methods) {
               if(method.getName().equals(methodName)) {
                  Class[] clazzs = method.getParameterTypes();
                   if(clazzs.length == arguments.length) {
                      description = method.getAnnotation(SystemServiceLog.class).description();
                       break;
                  }
              }
          }
          return description;
      }
    
    /**
     * getServiceMthodTableType:獲取注解中對(duì)方法的數(shù)據(jù)表類型 用于service層注解 . <br/>
     * @author lcma
     * @param joinPoint
     * @return
     * @throws Exception
     * @since JDK 1.7
     */
    private nt getServiceMthodTableType(JoinPoint joinPoint)
            throws Exception {
       String targetName = joinPoint.getTarget().getClass().getName();
       String methodName = joinPoint.getSignature().getName();
       Object[] arguments = joinPoint.getArgs();
       Class targetClass = Class.forName(targetName);
       Method[] methods = targetClass.getMethods();
       int tableType = 0;
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
               Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length) {
                tableType = method.getAnnotation(SystemServiceLog.class).tableType();
                    break;
               }
           }
       }
        return tableType;
   }
    
    /**
     * getServiceMthodParams:獲取json格式的參數(shù). <br/>
     * @author lcma
     * @param joinPoint
     * @return
     * @throws Exception
     * @since JDK 1.7
     */
    private String getServiceMthodParams(JoinPoint joinPoint)
            throws Exception {
       Object[] arguments = joinPoint.getArgs();
       String params = JacksonUtil.toJSon(arguments);
       return params;
   } 
}

需要注意的是,定義切點(diǎn)的時(shí)候,@Pointcut里面是自定義注解的路徑

每個(gè)切面?zhèn)鬟f的數(shù)據(jù)的都不一樣,最終決定,獲取切面的所有參數(shù),轉(zhuǎn)成json字符串,保存到數(shù)據(jù)庫(kù)中。

第三步

在service需要記錄日志的地方進(jìn)行注解,代碼如下:

@SystemServiceLog(description=Constants.ADMIN_SAVE_OPTIONS,tableType=Constants.ADMIM_TABLE_TYPE)

代碼圖片:

在常量類里面配置自定義注解的參數(shù)內(nèi)容:

第四步

把切面類所在的包路徑添加到Spring注解自動(dòng)掃描路徑下,并且啟動(dòng)對(duì)@AspectJ注解的支持,代碼如下:

<!-- 啟動(dòng)對(duì)@AspectJ注解的支持  --> 
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 自動(dòng)掃描包路徑  --> 
<context:component-scan base-package="com.iflytek.zhbs.common.aoplog" />
<context:component-scan base-package="com.iflytek.zhbs.service" />

最后數(shù)據(jù)庫(kù)記錄數(shù)據(jù)的效果如圖:

OK,功能已經(jīng)實(shí)現(xiàn),初次寫博客,寫的不好的地方請(qǐng)諒解。

多個(gè)注解可以合并成一個(gè),包括自定義注解

spring中有時(shí)候一個(gè)類上面標(biāo)記很多注解。

實(shí)際上Java注解可以進(jìn)行繼承(也就是把多個(gè)注解合并成1個(gè))

比如說(shuō)SpringMVC的注解

@RestController
@RequestMapping("/person")

可以合并為一個(gè)

@PathRestController("/user")

實(shí)現(xiàn)是:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RestController
@RequestMapping
public @interface PathRestController {
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中連接Mongodb進(jìn)行增刪改查的操作詳解

    Java中連接Mongodb進(jìn)行增刪改查的操作詳解

    MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),由C++語(yǔ)言編寫,旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案,本文給大家介紹了Java中連接Mongodb進(jìn)行操作,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-06-06
  • 學(xué)習(xí)Spring-Session+Redis實(shí)現(xiàn)session共享的方法

    學(xué)習(xí)Spring-Session+Redis實(shí)現(xiàn)session共享的方法

    本篇文章主要介紹了學(xué)習(xí)Spring-Session+Redis實(shí)現(xiàn)session共享的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名

    java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名

    這篇文章主要給大家介紹了關(guān)于java使用java.io.File類和java.nio.file.Path類對(duì)文件重命名的相關(guān)資料,本文僅為日常操作記錄,方便后期使用查找本地電腦文件太多了,又不想一個(gè)一個(gè)重命名,改名字什么的很麻煩,需要的朋友可以參考下
    2024-02-02
  • Java虛擬機(jī)JVM性能優(yōu)化(一):JVM知識(shí)總結(jié)

    Java虛擬機(jī)JVM性能優(yōu)化(一):JVM知識(shí)總結(jié)

    這篇文章主要介紹了Java虛擬機(jī)JVM性能優(yōu)化(一):JVM知識(shí)總結(jié),本文是系列文章的第一篇,后續(xù)篇章請(qǐng)繼續(xù)關(guān)注腳本之家,需要的朋友可以參考下
    2014-09-09
  • 詳解java中的6種單例寫法及優(yōu)缺點(diǎn)

    詳解java中的6種單例寫法及優(yōu)缺點(diǎn)

    在java中,單例有很多種寫法,面試時(shí),手寫代碼環(huán)節(jié),除了寫算法題,有時(shí)候也會(huì)讓手寫單例模式,這里記錄一下單例的幾種寫法和優(yōu)缺點(diǎn)。需要的朋友可以參考下
    2018-11-11
  • 在IDEA中使用debug工具去運(yùn)行java程序的實(shí)現(xiàn)步驟

    在IDEA中使用debug工具去運(yùn)行java程序的實(shí)現(xiàn)步驟

    調(diào)試工具(debug工具)是一種用于幫助程序員識(shí)別和修復(fù)程序中的錯(cuò)誤的工具,它們提供了一系列的功能,幫助程序員在代碼執(zhí)行的過(guò)程中跟蹤和檢測(cè)問(wèn)題,本文將給大家介紹使用debug工具去運(yùn)行java程序的實(shí)現(xiàn)步驟,需要的朋友可以參考下
    2024-04-04
  • JDBC建立數(shù)據(jù)庫(kù)連接的代碼

    JDBC建立數(shù)據(jù)庫(kù)連接的代碼

    這篇文章主要為大家詳細(xì)介紹了JDBC建立數(shù)據(jù)庫(kù)連接的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Spring Boot部署到Tomcat過(guò)程中遇到的問(wèn)題匯總

    Spring Boot部署到Tomcat過(guò)程中遇到的問(wèn)題匯總

    這篇文章主要給大家分享了關(guān)于Spring Boot部署到Tomcat過(guò)程中遇到的一些問(wèn)題,文中將解決的方法介紹非常詳細(xì),對(duì)同樣遇到這個(gè)問(wèn)題的朋友具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • 教你用Java在個(gè)人電腦上實(shí)現(xiàn)微信掃碼支付

    教你用Java在個(gè)人電腦上實(shí)現(xiàn)微信掃碼支付

    今天給大家?guī)?lái)的是Java實(shí)戰(zhàn)的相關(guān)知識(shí),文章圍繞著Java在個(gè)人電腦上實(shí)現(xiàn)微信掃碼支付展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • SpringBoot中的Aop用法示例詳解

    SpringBoot中的Aop用法示例詳解

    這篇文章主要介紹了SpringBoot中的Aop用法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12

最新評(píng)論