在spring配置
1编写自己的注解类
2.编写注解解析类
3.配置spring aop代理 (下面我使用注解 如使用配置 配置切点即可,有两种代理默认jdk代理 设置true 为cglib代理)
//注解类
1 /** 2 * 自定义注解 拦截器 3 * @author 4 * 给需要监控的方法加上改注解,就可以实现该方法的日志记录 5 */ 6 @Target({ ElementType.PARAMETER, ElementType.METHOD }) 7 @Retention(RetentionPolicy.RUNTIME) 8 @Documented 9 public @interface WbmsService{ 10 //描述 11 String description() default ""; 12 //操作类型 同步:sync 异步 async 13 String oprateType() default ""; 14 //客户名称 15 String clientName() default ""; 16 } 17 18 使用方法 在被调用的方法实现类上添加该注解 19 20 @WbmsService(description = "客户欠款查询", oprateType = "sync", clientName = "")
@Aspect @Component public class InterfaceRecord { // logService private IBizLogRecordService bizLogRecordService; // 初始化日志类 private static final Log logger = LogFactory.getLog(InterfaceRecord.class); // Service层切点 @Pointcut("@annotation(com.deppon.dpap.module.common.server.aop.WbmsService)") public void serviceAspect() { } @Before(value = "serviceAspect()") public void doBefore(JoinPoint joinPoint) { String now = DateUtil.getToday(); logger.info("接口拦截开始时间:" + now); } @AfterReturning(value = "serviceAspect()", argNames = "retVal", returning = "retVal") public void doAfterReturning(JoinPoint joinPoint, Object retVal) { String now = DateUtil.getToday(); logger.info("接口拦截结束时间:" + now); WbmsLogEntity log = new WbmsLogEntity(); try { // 补充数据 log = supplementEntity(log, joinPoint); // 返回参数 String responseStr = ""; if (retVal != null) { responseStr = JSON.toJSONString(retVal); } // 响应内容 log.setResponsContent(responseStr); // 成功状态 log.setIsSuccess(Constant.YES); // 保存到数据库 bizLogRecordService.saveWbmsLog(log); } catch (Exception e) { logger.error("==异常通知异常=="); logger.error("异常信息:{}", e); } } /** * @Title: doAfterThrowing * @Description: TODO 异常统一处理 * @param tags * @return return_type * @throws */ @AfterThrowing(pointcut = "serviceAspect()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { String now = DateUtil.getToday(); logger.error("接口异常拦截时间:" + now); logger.error("接口异常信息:" + e); WbmsLogEntity log = new WbmsLogEntity(); try { // 补充数据 log = supplementEntity(log, joinPoint); // 异常状态 log.setIsSuccess(Constant.NO); String responseStr = ""; if (e != null) { responseStr = JSON.toJSONString(e); } // 响应信息 log.setResponsContent(responseStr); // 保存到数据库 bizLogRecordService.saveWbmsLog(log); } catch (Exception ee) { logger.error("==异常通知异常=="); logger.error("异常信息:{}", ee); } } /** * @Title: supplementEntity * @Description: TODO 填充数据 * @param tags * @return return_type * @throws */ private WbmsLogEntity supplementEntity(WbmsLogEntity log, JoinPoint joinPoint) { try { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); // 请求的IP String ip = request.getRemoteAddr(); // 客户端IP log.setClientIp(ip); } catch (Exception ee) { // TODO: handle exception logger.error("获取不到httprequest:" + ee); } // 注解 Map<String, String> annos = getServiceMthodAnnotatin(joinPoint); // 客户名称 log.setClientName(annos.get("clientName")); // 创建时间 log.setCreateTime(new Date()); // 操作功能 String requestMethod = joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"; logger.info("请求方法:" + requestMethod); log.setOprateMethod(requestMethod); // 操作时间 log.setOprateTime(new Date()); // 操作类型 log.setOprateType(annos.get("oprateType")); // 操作描述 log.setOprateDes(annos.get("description")); // 请求参数 StringBuffer requestStr = new StringBuffer(); JSONArray jay = new JSONArray(); // 获取请求参数 if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) { for (int i = 0; i < joinPoint.getArgs().length; i++) { if (i != joinPoint.getArgs().length - 1) { requestStr.append(JSON.toJSONString(joinPoint.getArgs()[i])); requestStr.append(","); } else { requestStr.append(JSON.toJSONString(joinPoint.getArgs()[i])); } } } String arrayStr = "[" + requestStr + "]"; jay = JSON.parseArray(arrayStr); // 操作单据 // 操作人 List<String> createUserCodes = new ArrayList<String>(); // 操作人数据 createUserCodes.add("customerCode"); createUserCodes.add("createCode"); createUserCodes.add("operatePersoncode"); createUserCodes.add("disablePercode"); String createUserCode = JsonUtil.analysisJson(jay, createUserCodes); // 创建人 log.setCreateUserCode(createUserCode); // 修改人 log.setModifyUserCode(createUserCode); // 操作人 log.setOpratePer(createUserCode); // 请求参数 log.setRequestContent(requestStr.toString()); // 修改时间 log.setUpdateTime(new Date()); return log; } public void setBizLogRecordService(IBizLogRecordService bizLogRecordService) { this.bizLogRecordService = bizLogRecordService; } /** * 获取注解中对方法的描述信息 用于service层注解 * * @param joinPoint * 切点 * @return 方法描述 * @throws Exception */ @SuppressWarnings("rawtypes") public static Map<String, String> getServiceMthodAnnotatin( JoinPoint joinPoint) { // 结果 Map<String, String> result = new HashMap<String, String>(); // 获取target class名称 String targetName = joinPoint.getTarget().getClass().getName(); // 获取target method名称 String methodName = joinPoint.getSignature().getName(); // 获取请求参数 Object[] arguments = joinPoint.getArgs(); // 注解类 try { Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); // 注解方法 String description = ""; String oprateType = ""; String clientName = ""; for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) { description = method.getAnnotation(WbmsService.class) .description(); oprateType = method.getAnnotation(WbmsService.class) .oprateType(); clientName = method.getAnnotation(WbmsService.class) .clientName(); // 接口描述 result.put("description", description); // 操作类型 result.put("oprateType", oprateType); // 客户名称 result.put("clientName", clientName); break; } } } } catch (ClassNotFoundException e) { e.printStackTrace(); } return result; } }
<!-- aop激活自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>