Spring AOP實現(xiàn)復(fù)雜的日志記錄操作(自定義注解)
Spring AOP復(fù)雜的日志記錄(自定義注解)
做項目中,業(yè)務(wù)邏輯要求只要對數(shù)據(jù)庫數(shù)據(jù)進(jìn)行改動的都需要記錄日志(增刪改),記錄的內(nèi)容有操作者、操作的表名及表名稱、具體的操作,以及操作對應(yīng)的數(shù)據(jù)。
首先想到的就是Spring 的AOP功能。可是經(jīng)過一番了解過后,發(fā)現(xiàn)一般的日志記錄,只能記錄一些簡單的操作,例如表名、表名稱等記錄不到。
于是想到了自定義注解的方法,把想要記錄的內(nèi)容放在注解中,通過切入點來獲取注解參數(shù),就能獲取自己想要的數(shù)據(jù),記錄數(shù)據(jù)庫中。順著這個思路,在網(wǎng)上查找了一些相關(guān)資料,最終實現(xiàn)功能。話不多說,以下就是實現(xiàn)的思路及代碼:
第一步
在代碼中添加自定義注解,并且定義兩個屬性,一個是日志的描述(description),還有個是操作表類型(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ù)庫具體代碼如下:
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層切點 */ @Pointcut("@annotation(com.iflytek.zhbs.common.annotation.SystemServiceLog)") public void serviceAspect() { } /** * doServiceLog:獲取注解參數(shù),記錄日志. <br/> * @author lcma * @param joinPoint 切入點參數(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ù)庫日志 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:獲取注解中對方法的描述信息 用于service層注解 . <br/> * @author lcma * @param joinPoint 切點 * @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:獲取注解中對方法的數(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; } }
需要注意的是,定義切點的時候,@Pointcut里面是自定義注解的路徑
每個切面?zhèn)鬟f的數(shù)據(jù)的都不一樣,最終決定,獲取切面的所有參數(shù),轉(zhuǎn)成json字符串,保存到數(shù)據(jù)庫中。
第三步
在service需要記錄日志的地方進(jìn)行注解,代碼如下:
@SystemServiceLog(description=Constants.ADMIN_SAVE_OPTIONS,tableType=Constants.ADMIM_TABLE_TYPE)
代碼圖片:
在常量類里面配置自定義注解的參數(shù)內(nèi)容:
第四步
把切面類所在的包路徑添加到Spring注解自動掃描路徑下,并且啟動對@AspectJ注解的支持,代碼如下:
<!-- 啟動對@AspectJ注解的支持 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 自動掃描包路徑 --> <context:component-scan base-package="com.iflytek.zhbs.common.aoplog" /> <context:component-scan base-package="com.iflytek.zhbs.service" />
最后數(shù)據(jù)庫記錄數(shù)據(jù)的效果如圖:
OK,功能已經(jīng)實現(xiàn),初次寫博客,寫的不好的地方請諒解。
多個注解可以合并成一個,包括自定義注解
spring中有時候一個類上面標(biāo)記很多注解。
實際上Java注解可以進(jìn)行繼承(也就是把多個注解合并成1個)
比如說SpringMVC的注解
@RestController @RequestMapping("/person")
可以合并為一個
@PathRestController("/user")
實現(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 {}; }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中連接Mongodb進(jìn)行增刪改查的操作詳解
MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫,由C++語言編寫,旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲解決方案,本文給大家介紹了Java中連接Mongodb進(jìn)行操作,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-06-06學(xué)習(xí)Spring-Session+Redis實現(xiàn)session共享的方法
本篇文章主要介紹了學(xué)習(xí)Spring-Session+Redis實現(xiàn)session共享的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05java使用java.io.File類和java.nio.file.Path類對文件重命名
這篇文章主要給大家介紹了關(guān)于java使用java.io.File類和java.nio.file.Path類對文件重命名的相關(guān)資料,本文僅為日常操作記錄,方便后期使用查找本地電腦文件太多了,又不想一個一個重命名,改名字什么的很麻煩,需要的朋友可以參考下2024-02-02Java虛擬機(jī)JVM性能優(yōu)化(一):JVM知識總結(jié)
這篇文章主要介紹了Java虛擬機(jī)JVM性能優(yōu)化(一):JVM知識總結(jié),本文是系列文章的第一篇,后續(xù)篇章請繼續(xù)關(guān)注腳本之家,需要的朋友可以參考下2014-09-09在IDEA中使用debug工具去運(yùn)行java程序的實現(xiàn)步驟
調(diào)試工具(debug工具)是一種用于幫助程序員識別和修復(fù)程序中的錯誤的工具,它們提供了一系列的功能,幫助程序員在代碼執(zhí)行的過程中跟蹤和檢測問題,本文將給大家介紹使用debug工具去運(yùn)行java程序的實現(xiàn)步驟,需要的朋友可以參考下2024-04-04Spring Boot部署到Tomcat過程中遇到的問題匯總
這篇文章主要給大家分享了關(guān)于Spring Boot部署到Tomcat過程中遇到的一些問題,文中將解決的方法介紹非常詳細(xì),對同樣遇到這個問題的朋友具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03