這篇文章主要為大家展示了“Spring AOP如何實現復雜的日志記錄操作”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Spring AOP如何實現復雜的日志記錄操作”這篇文章吧。
做項目中,業務邏輯要求只要對數據庫數據進行改動的都需要記錄日志(增刪改),記錄的內容有操作者、操作的表名及表名稱、具體的操作,以及操作對應的數據。
首先想到的就是Spring 的AOP功能??墒墙涍^一番了解過后,發現一般的日志記錄,只能記錄一些簡單的操作,例如表名、表名稱等記錄不到。
于是想到了自定義注解的方法,把想要記錄的內容放在注解中,通過切入點來獲取注解參數,就能獲取自己想要的數據,記錄數據庫中。順著這個思路,在網上查找了一些相關資料,最終實現功能。話不多說,以下就是實現的思路及代碼:
在代碼中添加自定義注解,并且定義兩個屬性,一個是日志的描述(description),還有個是操作表類型(tableType),屬性參數可按需求改變。代碼如下:
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;
}定義切面類,獲取切面參數,保存數據庫具體代碼如下:
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:獲取注解參數,記錄日志. <br/>
* @author lcma
* @param joinPoint 切入點參數
* @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 {
//數據庫日志
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:獲取注解中對方法的數據表類型 用于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格式的參數. <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里面是自定義注解的路徑
每個切面傳遞的數據的都不一樣,最終決定,獲取切面的所有參數,轉成json字符串,保存到數據庫中。
在service需要記錄日志的地方進行注解,代碼如下:
@SystemServiceLog(description=Constants.ADMIN_SAVE_OPTIONS,tableType=Constants.ADMIM_TABLE_TYPE)
代碼圖片:

在常量類里面配置自定義注解的參數內容:


把切面類所在的包路徑添加到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" />
最后數據庫記錄數據的效果如圖:

OK,功能已經實現,初次寫博客,寫的不好的地方請諒解。
spring中有時候一個類上面標記很多注解。
實際上Java注解可以進行繼承(也就是把多個注解合并成1個)
@RestController
@RequestMapping("/person")可以合并為一個
@PathRestController("/user")實現是:
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 {};
}以上是“Spring AOP如何實現復雜的日志記錄操作”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。