1. 程式人生 > >java如何實現一個切面(儲存日誌)

java如何實現一個切面(儲存日誌)

package com.cloudtech.web.aop;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cloudtech.web.dao.AdminMapper;
import com.cloudtech.web.dao.AuthorityMapper;
import com.cloudtech.web.dao.FieldConfigMapper;
import com.cloudtech.web.dao.OperationDetailLogsMapper;
import com.cloudtech.web.dao.OperationLogsMapper;
import com.cloudtech.web.dao.OperatorMapper;
import com.cloudtech.web.dao.RoleMapper;
import com.cloudtech.web.dao.SystemConfigMapper;
import com.cloudtech.web.entity.Admin;
import com.cloudtech.web.entity.Authority;
import com.cloudtech.web.entity.FieldConfig;
import com.cloudtech.web.entity.OperationDetailLogs;
import com.cloudtech.web.entity.OperationLogs;
import com.cloudtech.web.entity.Operator;
import com.cloudtech.web.entity.Role;
import com.cloudtech.web.entity.SystemConfig;
import com.cloudtech.web.enums.ChildModuleType;
import com.cloudtech.web.enums.OperationType;
import com.cloudtech.web.enums.ParentModuleType;
import com.cloudtech.web.vo.FieldConfigVo;
import com.cloudtech.web.vo.Principal;

/**
 * 
* @ClassName: LogAspect  
* @Description:  日誌操作記錄 
* @author wude  
* @date 2018年7月19日  
*
 */
@Aspect
@Component
public class LogAspect {
	/**
	 * @serial 日誌.
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
	@Autowired
	private OperationLogsMapper logsMapper;
	@Autowired
	private OperationDetailLogsMapper detailLogsMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private AuthorityMapper authorityMapper;
	@Autowired
	private OperatorMapper operatorMapper;
	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private SystemConfigMapper systemConfigMapper;
	@Autowired
	private FieldConfigMapper fieldConfigMapper;

	/**
	 * 角色日誌修改和新增操作日誌記錄
	 * @param point
	 * @throws Throwable
	 */
	@Around("execution(* com.cloudtech.web.service.impl.RoleServiceImpl.insertOrUpdate(..))")
	public Object processInsertRoleConfig(ProceedingJoinPoint point) throws Throwable {
		Object rvt = null;
		Role role = null;
		Principal principal = null;  //新增
		OperationType type = null;   //操作型別 
		String title = "";      //標題
		// 訪問目標方法的引數:
		Object[] args = point.getArgs(); // point.getArgs() 獲得執行引數
		//父模組id
		Integer module = ParentModuleType.ADMIN_MANGGER.getCode();
		//子模組id
		Integer subModule = ChildModuleType.ROLE_LIST.getCode();
		if (args != null && args.length > 0) {
			role = (Role) args[0];
			principal = (Principal) args[1];
			type = (OperationType) args[2];
			
			if(type == OperationType.ADD) {    //新增
				rvt = point.proceed();
				LOGGER.debug("新增角色操作日誌記錄: " + role.toString());
				if(role.getId() != null){
					//插入日誌表
					title = "角色資訊新增";
					OperationLogs logs = insertLog(title, principal, type,module,subModule);
					
					//插入日誌詳情表-----角色名稱
					insertRoleDetailLog(logs, null, role, 1, type);
					
					//插入日誌詳情表-----角色描述
					insertRoleDetailLog(logs, null, role, 2, type);
				}
			}
			
			Role beforeRole = null;   //修改之前的role
			if(type == OperationType.UPDATE) {    //修改
				beforeRole = roleMapper.selectByPrimaryKey(role.getId());
				
				LOGGER.debug("修改角色操作日誌記錄: " + role.toString());
				rvt = point.proceed();
				
				boolean flag = false;
				boolean nameUpdate = false;    //名字修改
				boolean descUpdate = false;    //描述 修改
				if (!beforeRole.getName().equals(role.getName())) {
					flag = true;
					nameUpdate = true;
				}
				
				if (!beforeRole.getDescription().equals(role.getDescription())) {
					flag = true;
					descUpdate = true;
				}
				
				if(flag){   //修改過
					title = "角色資訊修改";
					OperationLogs logs = insertLog(title, principal, type,module,subModule);
					
					if(nameUpdate){   //名字修改
						//插入日誌詳情表-----角色名稱
						insertRoleDetailLog(logs, beforeRole, role, 1, type);
					}
					
					if(descUpdate){    //描述 修改
						//插入日誌詳情表-----角色描述
						insertRoleDetailLog(logs, beforeRole, role, 2, type);
					}
				}
			}
		}
		return rvt;
	}
	
	
	/**
	 * 角色模組刪除操作日誌
	 * @param point
	 * @return
	 * @throws Throwable
	 */
	@Around("execution(* com.cloudtech.web.service.impl.RoleServiceImpl.delete(..))")
	public Object processDeleteRoleLog(ProceedingJoinPoint point) throws Throwable {
		Object[] args = point.getArgs();
		Integer id = null;
		Principal principal = null;  //新增
		OperationType type = null;   //操作型別 
		String title = "";      //標題
		//父模組id
		Integer module = ParentModuleType.ADMIN_MANGGER.getCode();
		//子模組id
		Integer subModule = ChildModuleType.ROLE_LIST.getCode();
		if (args != null && args.length > 0) {
			id = (Integer) args[0];
			principal = (Principal) args[1];
			type = (OperationType) args[2];
			
			LOGGER.debug("刪除角色操作日誌記錄: 角色id為" + id);
			
			//插入日誌表
			title = "角色資訊刪除";
			OperationLogs logs = insertLog(title, principal, type,module,subModule);
			
			Role beforeRole = roleMapper.selectByPrimaryKey(id);
			
			//角色名稱
			insertRoleDetailLog(logs, beforeRole, null, 1, type);
			//角色描述
			insertRoleDetailLog(logs, beforeRole, null, 2, type);
		}
		// 用改變後的引數執行目標方法
		return point.proceed(args);
	}
	
	
	
	public static void main(String[] args) {
		 String a="#123";
		 System.out.println(a.substring(1,a.length()));
	}
}

@Around  環繞增強

注意:涉及到增加和修改操作,選用瞭解rvt = point.proceed();的用法。增加的時候,先設定這個的意思就是,先跑新增的方法,再判斷id是否有值,這樣才能確認新增成功勒,而,修改需要儲存到日誌中,需要對比修改前和修改後的物件,所以在先查詢後,再呼叫point.proceed()方法