java如何实现一个切面(保存日志)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16855077/article/details/84336774
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()方法

猜你喜欢

转载自blog.csdn.net/qq_16855077/article/details/84336774
今日推荐