Spring boot學習(六)Spring boot實現AOP記錄操做日誌

前言

在實際的項目中,特別是管理系統中,對於那些重要的操做咱們一般都會記錄操做日誌。好比對數據庫的CRUD操做,咱們都會對每一次重要的操做進行記錄,一般的作法是向數據庫指定的日誌表中插入一條記錄。這裏就產生了一個問題,難道要咱們每次在 CRUD的時候都手動的插入日誌記錄嗎?這確定是不合適的,這樣的操做無疑是加大了開發量,並且不易維護,因此實際項目中老是利用AOP(Aspect Oriented Programming)即面向切面編程這一技術來記錄系統中的操做日誌。html

文章首發於我的博客:【www.xiongfrblog.cnjava

日誌分類

這裏我把日誌按照面向的對象不一樣分爲兩類:web

  • 面向用戶的日誌:用戶是指使用系統的人,這一類日誌一般記錄在數據庫裏邊,而且一般是記錄對數據庫的一些CRUD操做。
  • 面向開發者的日誌:查看這一類日誌的通常都是開發人員,這類日誌一般保存在文件或者在控制檯打印(開發的時候在控制檯,項目上線以後以後保存在文件中),這一類日誌主要用於開發者開發時期和後期維護時期定位錯誤。

面向不一樣對象的日誌,咱們採用不一樣的策略去記錄。很容易看出,對於面向用戶的日誌具備很強的靈活性,須要開發者控制用戶的哪些操做須要向數據庫記錄日誌,因此這一類保存在數據庫的日誌咱們在使用 AOP記錄時用自定義註解的方式去匹配;而面向開發者的日誌咱們則使用表達式去匹配就能夠了(這裏有可能敘述的有點模糊,看了下面去案例將會很清晰),下面分別介紹兩種日誌的實現。spring

實現AOP記錄面向用戶的日誌

接下來分步驟介紹Spring boot中怎樣實現經過AOP記錄操做日誌。sql

添加依賴

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一個名爲LogAnnotation文件,文件內容以下:springboot

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一下【傳送門】。bash

準備數據庫日誌表以及實體類,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.682  INFO 3832 --- [nio-8080-exec-1] c.web.springbootaoplog.config.LogAsPect  : 我在目標方法以前執行!
2019-01-24 22:02:17.688  INFO 3832 --- [nio-8080-exec-1] c.w.s.controller.HomeController          : 我被執行了!
2019-01-24 22:02:17.689  INFO 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.108  INFO 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~

相關文章
相關標籤/搜索