Spring boot學習(六)Spring boot實現AOP記錄操作日誌
前言
在實際的專案中,特別是管理系統中,對於那些重要的操作我們通常都會記錄操作日誌。比如對資料庫的 CRUD
操作,我們都會對每一次重要的操作進行記錄,通常的做法是向資料庫指定的日誌表中插入一條記錄。這裡就產生了一個問題,難道要我們每次在 CRUD
的時候都手動的插入日誌記錄嗎?這肯定是不合適的,這樣的操作無疑是加大了開發量,而且不易維護,所以實際專案中總是利用 AOP(Aspect Oriented Programming)
即面向切面程式設計這一技術來記錄系統中的操作日誌。
日誌分類
這裡我把日誌按照面向的物件不同分為兩類:
CRUD
面向不同物件的日誌,我們採用不同的策略去記錄。很容易看出,對於面向使用者的日誌具有很強的靈活性,需要開發者控制使用者的哪些操作需要向資料庫記錄日誌,所以這一類儲存在資料庫的日誌我們在使用 AOP
記錄時用自定義註解的方式去匹配;而面向開發者的日誌我們則使用表示式去匹配就可以了(這裡有可能敘述的有點模糊,看了下面去案例將會很清晰),下面分別介紹兩種日誌的實現。
實現AOP記錄面向使用者的日誌
接下來分步驟介紹 Spring boot
中怎樣實現通過 AOP
記錄操作日誌。
新增依賴
在 pom.xml
檔案中新增如下依賴:
<!-- aop依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
修改配置檔案
在專案的 application.properties
檔案中新增下面一句配置:
spring.aop.auto=true
這裡特別說明下,這句話不加其實也可以,因為預設就是 true
,只要我們在 pom.xml
中添加了依賴就可以了,這裡提出來是讓大家知道有這個有這個配置。
自定義註解
上邊介紹過了了,因為這類日誌比較靈活,所以我們需要自定義一個註解,使用的時候在需要記錄日誌的方法上新增這個註解就可以了,首先在啟動類的同級包下邊新建一個 config
包,在這個報下邊新建 new
一個名為 Log
的 Annotation
檔案,檔案內容如下:
package com.web.springbootaoplog.config; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author Promise * @createTime 2018年12月18日 下午9:26:25 * @description定義一個方法級別的@log註解 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Log { String value() default ""; }
這裡用到的是 Java
元註解的相關知識,不清楚相關概念的朋友可以去這篇部落格 get
一下【 傳送門 】。
準備資料庫日誌表以及實體類,sql介面,xml檔案
既然是向資料庫中插入記錄,那麼前提是需要建立一張記錄日誌的表,下面給出我的表 sql
,由於是寫樣例,我這裡這張表設計的很簡單,大家可以自行設計。
CREATE TABLE `sys_log` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵', `user_id` int(11) NOT NULL COMMENT '操作員id', `user_action` varchar(255) NOT NULL COMMENT '使用者操作', `create_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '建立時間', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COMMENT='日誌記錄表';
通過上篇部落格介紹的 MBG
生成相應的實體類, sql
介面檔案,以及 xml
檔案,這裡不再概述,不清楚的朋友請移步【 傳送門 】
當然還需要建立 service
介面檔案以及介面實現類,這裡直接給出程式碼:
ISysLogServcie.java
package com.web.springbootaoplog.service; import com.web.springbootaoplog.entity.SysLog; /** * @author Promise * @createTime 2018年12月18日 下午9:29:48 * @description 日誌介面 */ public interface ISysLogService { /** * 插入日誌 * @param entity * @return */ int insertLog(SysLog entity); }
SysLogServiceImpl.java
package com.web.springbootaoplog.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.web.springbootaoplog.config.Log; import com.web.springbootaoplog.dao.SysLogMapper; import com.web.springbootaoplog.entity.SysLog; import com.web.springbootaoplog.service.ISysLogService; /** * @author Promise * @createTime 2018年12月18日 下午9:30:57 * @description */ @Service("sysLogService") public class SysLogServiceImpl implements ISysLogService{ @Autowired private SysLogMapper sysLogMapper; @Override public int insertLog(SysLog entity) { // TODO Auto-generated method stub return sysLogMapper.insert(entity); } }
AOP的切面和切點
準備上邊的相關檔案後,下面來介紹重點--建立 AOP
切面實現類,同樣我們這裡將該類放在 config
包下,命名為 LogAsPect.java
,內容如下:
package com.web.springbootaoplog.config; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Date; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.hibernate.validator.internal.util.logging.LoggerFactory; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.LocalVariableTableParameterNameDiscoverer; import org.springframework.stereotype.Component; import com.web.springbootaoplog.entity.SysLog; import com.web.springbootaoplog.service.ISysLogService; /** * @author Promise * @createTime 2018年12月18日 下午9:33:28 * @description 切面日誌配置 */ @Aspect @Component public class LogAsPect { private final static Logger log = org.slf4j.LoggerFactory.getLogger(LogAsPect.class); @Autowired private ISysLogService sysLogService; //表示匹配帶有自定義註解的方法 @Pointcut("@annotation(com.web.springbootaoplog.config.Log)") public void pointcut() {} @Around("pointcut()") public Object around(ProceedingJoinPoint point) { Object result =null; long beginTime = System.currentTimeMillis(); try { log.info("我在目標方法之前執行!"); result = point.proceed(); long endTime = System.currentTimeMillis(); insertLog(point,endTime-beginTime); } catch (Throwable e) { // TODO Auto-generated catch block } return result; } private void insertLog(ProceedingJoinPoint point ,long time) { MethodSignature signature = (MethodSignature)point.getSignature(); Method method = signature.getMethod(); SysLog sys_log = new SysLog(); Log userAction = method.getAnnotation(Log.class); if (userAction != null) { // 註解上的描述 sys_log.setUserAction(userAction.value()); } // 請求的類名 String className = point.getTarget().getClass().getName(); // 請求的方法名 String methodName = signature.getName(); // 請求的方法引數值 String args = Arrays.toString(point.getArgs()); //從session中獲取當前登陸人id //Long useride = (Long)SecurityUtils.getSubject().getSession().getAttribute("userid"); Long userid = 1L;//應該從session中獲取當前登入人的id,這裡簡單模擬下 sys_log.setUserId(userid); sys_log.setCreateTime(new java.sql.Timestamp(new Date().getTime())); log.info("當前登陸人:{},類名:{},方法名:{},引數:{},執行時間:{}",userid, className, methodName, args, time); sysLogService.insertLog(sys_log); } }
這裡簡單介紹下關於 AOP
的幾個重要註解:
-
@Aspect
:這個註解表示將當前類視為一個切面類 -
@Component
:表示將當前類交由Spring
管理。 -
@Pointcut
:切點表示式,定義我們的匹配規則,上邊我們使用@Pointcut("@annotation(com.web.springbootaoplog.config.Log)")
表示匹配帶有我們自定義註解的方法。 -
@Around
:環繞通知,可以在目標方法執行前後執行一些操作,以及目標方法丟擲異常時執行的操作。
我們用到的註解就這幾個,當然還有其他的註解,這裡我就不一一介紹了,想要深入瞭解 AOP
相關知識的朋友可以移步官方文件【 傳送門 】
下面看一段關鍵的程式碼:
log.info("我在目標方法之前執行!"); result = point.proceed(); long endTime = System.currentTimeMillis(); insertLog(point,endTime-beginTime);
其中 result = point.proceed();
這句話表示執行目標方法,可以看出我們在這段程式碼執行之前列印了一句日誌,並在執行之後呼叫了 insertLog()
插入日誌的方法,並且在方法中我們可以拿到目標方法所在的類名,方法名,引數等重要的資訊。
測試控制器
在 controller
包下新建一個 HomeCOntroller.java
(名字大家隨意),內容如下:
package com.web.springbootaoplog.controller; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.web.springbootaoplog.config.Log; import com.web.springbootaoplog.entity.SysLog; import com.web.springbootaoplog.service.ISysLogService; /** * @author Promise * @createTime 2019年1月2日 下午10:35:30 * @description測試controller */ @Controller public class HomeController { private final static Logger log = org.slf4j.LoggerFactory.getLogger(HomeController.class); @Autowired private ISysLogService logService; @RequestMapping("/aop") @ResponseBody @Log("測試aoplog") public Object aop(String name, String nick) { Map<String, Object> map =new HashMap<>(); log.info("我被執行了!"); map.put("res", "ok"); return map; } }
定義一個測試方法,帶有兩個引數,並且為該方法添加了我們自定義的 @Log
註解,啟動專案,瀏覽器訪問 localhost:8080/aop?name=xfr&nick=eran
,這時候檢視 eclipse
控制檯的部分輸出資訊如下:
2019-01-24 22:02:17.682INFO 3832 --- [nio-8080-exec-1] c.web.springbootaoplog.config.LogAsPect: 我在目標方法之前執行! 2019-01-24 22:02:17.688INFO 3832 --- [nio-8080-exec-1] c.w.s.controller.HomeController: 我被執行了! 2019-01-24 22:02:17.689INFO 3832 --- [nio-8080-exec-1] c.web.springbootaoplog.config.LogAsPect: 當前登陸人:1,類名:com.web.springbootaoplog.controller.HomeController,方法名:aop,引數:[xfr, eran],執行時間:6
可以看到我們成功在目標方法執行前後插入了一些邏輯程式碼,現在再看資料庫裡邊的資料:

在這裡插入圖片描述
成功記錄了一條資料。
實現AOP記錄面向開發者的日誌
首先這裡我列舉一個使用該方式的應用場景,在專案中出現了 bug
,我們想要知道前臺的請求是否進入了我們控制器中,以及引數的獲取情況,下面開始介紹實現步驟。
其實原理跟上邊是一樣的,只是切點的匹配規則變了而已,而且不用將日誌記錄到資料庫,打印出來即可。
首先在 LogAsPect.java
中定義一個新的切點表示式,如下:
@Pointcut("execution(public * com.web.springbootaoplog.controller..*.*(..))") public void pointcutController() {}
@Pointcut("execution(public * com.web.springbootaoplog.controller..*.*(..))")
表示匹配 com.web.springbootaoplog.controller
包及其子包下的所有公有方法。
關於這個表示式詳細的使用方法可以移步這裡,【 傳送門 】
再新增匹配到方法時我們要做的操作:
@Before("pointcutController()") public void around2(JoinPoint point) { //獲取目標方法 String methodNam = point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName(); //獲取方法引數 String params = Arrays.toString(point.getArgs()); log.info("get in {} params :{}",methodNam,params); }
@Before
:表示目標方法執行之前執行以下方法體的內容。
再在控制器中新增一個測試方法:
@RequestMapping("/testaop3") @ResponseBody public Object testAop3(String name, String nick) { Map<String, Object> map = new HashMap<>(); map.put("res", "ok"); return map; }
可以看到這個方法我們並沒有加上 @Log
註解,重啟專案,瀏覽器訪問localhost:8080/testaop3?name=xfr&nick=eran,這時候檢視eclipse控制檯的部分輸出資訊如下:
2019-01-24 23:19:49.108INFO 884 --- [nio-8080-exec-1] c.web.springbootaoplog.config.LogAsPect: get in com.web.springbootaoplog.controller.HomeController.testAop3 params :[xfr, eran]
打印出了關鍵日誌,這樣我們就能知道是不是進入了該方法,引數獲取是否正確等關鍵資訊。
這裡有的朋友或許會有疑問這樣會不會與添加了 @Log
的方法重複了呢,的確會,所以在專案中我通常都將 @Log
註解用在了 Service
層的方法上,這樣也更加合理。
結語
好了,關於 Aop
記錄日誌的內容就介紹這麼多了,下一篇部落格再見。bye~