代码被修改之后上传,仅供参考实现逻辑
在配置文件中配置AOP切点
<!-- 声明一个日志类 -->
<bean id="logManager" class="com.iflytek.swk.ghj.utils.common.LogPointcutManager"></bean>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* com.iflytek.swk.ghj.web.*.*Controller.*(..))" id="logPointcut"/>
<!-- 新增的切面,做日志管理插入数据库操作使用 -->
<aop:aspect id="log" ref="logManager" order="3">
<aop:after-returning method="afterReturningSuccess" returning="returnObj" pointcut-ref="logPointcut"/>
</aop:aspect>
<!--异常切面 -->
<aop:aspect id="ex1" ref="logManager">
<aop:after-throwing method="afterThrowingError" throwing="ex" pointcut-ref="logPointcut" />
</aop:aspect>
</aop:config>
实现代码
package com.iflytek.swk.ghj.utils.common;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.swk.ghj.base.SysCode;
import com.iflytek.swk.ghj.base.common.ModelEnumConstant;
import com.iflytek.swk.ghj.common.user.service.LoginUserInfoService;
import com.iflytek.swk.ghj.model.system.LogInfo;
import com.iflytek.swk.ghj.model.usr.UserInfo;
import com.iflytek.swk.ghj.service.system.LogInfoService;
/**
* ClassName: LogPointcutManager <br/>
* Function: 执行日志记录. <br/>
* date: 2018年6月1日 下午3:00:33 <br/>
* @author hanxu
* @version 0.2
*/
public class LogPointcutManager
{
/**
* 日志记录Service
*/
@Autowired
private LogInfoService logInfoService;
/**
* 获取用户信息接口
*/
@Autowired
private LoginUserInfoService loginUserInfoService;
/**
* 操作成功时记录操作的方法
*
* @param point JoinPoint
* @param returnObj Object
*/
public void afterReturningSuccess(JoinPoint point, Object returnObj)
{
//方法名
String method = point.getSignature().getName();
//包名+类名
String modelName = point.getTarget().getClass().getName();
//包名+类名+方法名
String str = modelName + "." + method;
// 目标对象不为日志模块Service
if (!method.startsWith("to") && !"updateDeptAndUser".equals(method)
&& !SysCode.AOP_OUT_LOG.contains(point.getTarget().getClass().getName()))
{
for (ModelEnumConstant ec : ModelEnumConstant.values())
{
if (str.equals(ec.getValue()))
{
// 用户信息
UserInfo userInfo = this.getUserInfo();
if (userInfo != null)
{
// 操作类型
Integer actType = ec.getType();
// 操作模块
String actModule = ec.getClassName();
// 操作功能(操作方法)
String actAction = ec.getMethod();
// 操作结果
String actResult = "成功";
if (returnObj != null)
{
try
{
JSONObject json = JSONObject.parseObject(returnObj.toString());
if (StringUtils.isNotBlank(json.getString("flag")) && !json.getBoolean("flag"))
{
actResult = "失败";
}
}
catch (Exception e)
{
actResult = "失败:解析json失败";
}
}
String actMessage = method + "方法" + actType + "成功!";
//保存日志
this.saveLogInfo(point.getArgs(),actMessage,actModule,actResult, actAction,actType,userInfo);
}
}
}
}
}
/**
* 操作失败时,记录失败异常的方法
*
* @param point
* JoinPoint
* @param ex
* Throwable
*/
public void afterThrowingError(JoinPoint point, Throwable ex)
{
String method = point.getSignature().getName();
String modelName = point.getTarget().getClass().getName();
String str = modelName + "." + method;
for (ModelEnumConstant ec : ModelEnumConstant.values())
{
if (str.equals(ec.getValue()))
{
// 用户信息
UserInfo userInfo = this.getUserInfo();
if (userInfo != null)
{
// 操作类型
Integer actType = ec.getType();
// 操作模块
String actModule = ec.getClassName();
// 操作功能(操作方法)
String actAction = ec.getMethod();
// 异常信息(操作结果)
String actResult = "失败";
//错误信息
String actMessage = ex.getMessage();
//保存日志
this.saveLogInfo(point.getArgs(), actMessage, actModule, actResult, actAction, actType, userInfo);
}
}
}
}
/**
* saveLogInfo:保存日志信息. <br/>
* @param params 操作方法参数
* @param actMessage 操作内容
* @param actModule 操作模块
* @param actResult 操作结果
* @param actAction 操作方法名
* @param actType 操作类型
* @param userInfo 用户信息
*/
private void saveLogInfo(Object[] params, String actMessage, String actModule, String actResult, String actAction,
Integer actType, UserInfo userInfo)
{
LogInfo logInfo = new LogInfo();
String actObj = this.getActObj(params);
//判断操作内容超长
if (StringUtils.isNotBlank(actMessage) && actMessage.length() >= 500)
{
actMessage = actMessage.substring(0, 500);
}
logInfo.setActModule(actModule);
logInfo.setActObj(actObj);
logInfo.setActResult(actResult);
logInfo.setUserId(userInfo.getUserId().toString());
logInfo.setActMessage(actMessage);
logInfo.setActAction(actAction);
logInfo.setActorName(userInfo.getName());
logInfo.setActType(actType);
logInfoService.add(logInfo);
}
/**
* getUserInfo:获取用户信息. <br/>
* @return UserInfo
*/
private UserInfo getUserInfo()
{
// request对象
HttpServletRequest request =
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
return loginUserInfoService.getLoginUserInfo(request);
}
/**
* getActObj:获取操作对象内容. <br/>
* @param params
* @return
*/
private String getActObj(Object[] params)
{
//解析参数对象
StringBuffer methodParams = new StringBuffer(128);
if (!ArrayUtils.isEmpty(params))
{
for (Object object : params)
{
methodParams.append("[");
if (null != object)
{
methodParams.append(object.toString());
}
methodParams.append("]");
}
}
//判断操作对象内容超长
String actObj = methodParams.toString();
if (actObj.length() >= 2048)
{
actObj = actObj.substring(0, 2048);
}
return actObj;
}
}
需要 拦截的方法
package com.iflytek.swk.ghj.base.common;
public enum ModelEnumConstant {
/**
* 来件模块
*/
inCaselistInCase("com.iflytek.swk.ghj.web.cse.InCaseController.listByCondition","发件","查询来件列表",4),
inCase("com.iflytek.swk.ghj.web.cse.InCaseController.formalReply","来件","启动正式回复",5),
inCaseupdate("com.iflytek.swk.ghj.web.cse.InCaseController.update","来件","更新来件信息",3),
/**
* 发件模块
*/
outCaseAdd("com.iflytek.swk.ghj.web.cse.OutCaseController.addOutCase","发件","新增发件",1),
outCaseAdd("com.iflytek.swk.ghj.web.cse.OutCaseController.addOutCase","发件","新增发件",1);
private String value;
private String method;
/**
*保存进数据库,页面展示关联字典表显示
*/
private Integer type;
private String className;
ModelEnumConstant(String value,String className,String method,Integer type){
this.value=value;
this.method=method;
this.type=type;
this.className=className;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
被修改只有只是用来参考实现的逻辑