1.命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
2.配置xml
<!-- aop支持 如果记录Controller aop配置必须写到spring mvc的xml中,如果记录service aop 配置要写到 spring主配置文件-->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- aop 实现记录日志功能 -->
<bean id="logAspect" class="com.rdkl.qmjs.logs.LogAspect"/>
<context:component-scan base-package="com.rdkl.qmjs.controller" />
3.实体类
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface RecordLog { //描述 public String description() default ""; //模块Id public String module_id() default ""; //日志等级 public String log_lev() default "1"; //记录字段 public String recordfield() default ""; //字段描述 public String fieldnote() default ""; //操作类型:0-add-高,1-update-高,2-delete-高,3-login-中,4-select-低 public String oper_type() default ""; //系统类型 0:管理平台 1:场馆后台 2:商城后台 public int sys_type() default 0; }
@Aspect public class LogAspect { @Autowired private LogService logService; //方法执行的前后调用 @AfterReturning("execution (* com.rdkl.qmjs.controller..*.*(..))&& @annotation(recordLog)") public void opLogAfterReturning(JoinPoint joinPoint,RecordLog recordLog) throws Throwable{ Object[] params = joinPoint.getArgs();//获取目标方法体参数 System.out.println("*************write log begin****************"); HttpServletRequest request=null; String ip=""; if(params!=null&¶ms.length>0){ for(Object obj:params){ if(obj instanceof HttpServletRequest){ request=(HttpServletRequest)obj; ip=getIpAddr(request); } } } String desc=recordLog.description(); String field=recordLog.recordfield(); String note=recordLog.fieldnote(); int sys_type = recordLog.sys_type(); if(request!=null&&StringUtils.isNotBlank(field)){ String fieldVal=request.getParameter(field); if(fieldVal!=null&&!"".equals(fieldVal)){ if(StringUtils.isNotBlank(note)){ desc+="-"+note+":"+fieldVal; }else{ desc+="-"+field+":"+fieldVal; } } } User user= SecurityCommonUtils.getCurrentUser(); Log log=new Log(); log.setIp_addr(ip); if("0".equals(recordLog.oper_type())||"1".equals(recordLog.oper_type()) ||"2".equals(recordLog.oper_type())){//增删改数据,等级为高 log.setLog_lev(2);//高级 }else if("3".equals(recordLog.oper_type())){ log.setLog_lev(1);//中级 }else{ log.setLog_lev(0);//普通 } String module_id=recordLog.module_id(); if(StringUtils.isNotBlank(module_id)){ log.setModule_id(Long.parseLong(module_id));//模块ID } log.setOper_date(new Date()); log.setContent(desc); log.setSys_type(sys_type); if(sys_type==1){//场馆日志 log.setObject_id(user.getRel_module_id()); } if(user!=null){ log.setUser_id(user.getUser_id()); log.setOper_user(user.getUser_id()); } logService.insertLogs(log); System.out.println("*************write log end****************"); } /** * 获取请求的IP * @param request * @return */ private String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } }
在需要记录操作的方法上
@RecordLog(description = "保存增加的信息", module_id = "1114003001", oper_type = "0", sys_type = 1)