Spring AOP實現(xiàn)復雜的日志記錄操作(自定義注解)
Spring AOP復雜的日志記錄(自定義注解)
做項目中,業(yè)務邏輯要求只要對數(shù)據(jù)庫數(shù)據(jù)進行改動的都需要記錄日志(增刪改),記錄的內(nèi)容有操作者、操作的表名及表名稱、具體的操作,以及操作對應的數(shù)據(jù)。
首先想到的就是Spring 的AOP功能??墒墙?jīng)過一番了解過后,發(fā)現(xiàn)一般的日志記錄,只能記錄一些簡單的操作,例如表名、表名稱等記錄不到。
于是想到了自定義注解的方法,把想要記錄的內(nèi)容放在注解中,通過切入點來獲取注解參數(shù),就能獲取自己想要的數(shù)據(jù),記錄數(shù)據(jù)庫中。順著這個思路,在網(wǎng)上查找了一些相關資料,最終實現(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需要記錄日志的地方進行注解,代碼如下:
@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中有時候一個類上面標記很多注解。
實際上Java注解可以進行繼承(也就是把多個注解合并成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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
基于Java Callable接口實現(xiàn)線程代碼實例
這篇文章主要介紹了基于Java Callable接口實現(xiàn)線程代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
PowerJob的DispatchStrategy方法工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的DispatchStrategy方法工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
mybatis寫xml時數(shù)字類型千萬別用 !=‘‘(不為空串)進行判斷的示例詳解
這篇文章主要介紹了mybatis寫xml時數(shù)字類型千萬別用 !=‘‘(不為空串)進行判斷的示例詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
SpringBoot使用validation-api實現(xiàn)參數(shù)校驗的示例
這篇文章主要介紹了SpringBoot使用validation-api實現(xiàn)參數(shù)校驗的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

