AOP implementa aspecto de log

AOP é chamada de programação orientada a aspectos.É um paradigma de programação cujo objetivo é melhorar a modularidade do código. O Spring AOP é implementado com base no
proxy dinâmico. Se a interface for implementada, o proxy dinâmico JDK será usado. Caso contrário, o proxy CGLIB será usado. A aplicação
do AOP no Spring é refletida principalmente na transação, log, tratamento de exceções e outros aspectos. Ao fazer algum processamento aprimorado antes e depois do código, o isolamento da lógica de negócios pode ser realizado, a capacidade de modularização do código pode ser melhorada e também pode ser descongelada
. O Spring fornece principalmente
métodos de implementação, como aspecto Aspect, ponto de conexão JoinPoint, ponto de entrada PointCut e aprimoramento Advice.

// 第一个* 代表任意返回值
// 第二个* 当表任意方法
//(..) 代表任意参数
// 匹配controller下面的所有共有方法。
@Pointcut("execution( public * com.uaf.xxx.controller..*.*(..))")

código de aspecto


import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author Fly
 * @filename WebLogAcpect
 * @description 日志切面
 * @date 2019/12/27 16:56
 */
@Aspect
@Component
public class WebLogAcpect {
    
    
	/**
	 * 切点
	 */
	@Pointcut("execution(public *  com.uaf.xxx.controller..*.*(..))")
	public void pointCut() {
    
    
	}

	@Before("pointCut()")
	public void before(JoinPoint joinPoint) throws Throwable {
    
    
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletResponse response = attributes.getResponse();
		// 获取切入的 Method
		MethodSignature joinPointObject = (MethodSignature) joinPoint.getSignature();

		Method method = joinPointObject.getMethod();
		if (method.isAnnotationPresent(PostMapping.class)) {
    
    
			/** 值*/
			Object[] args = joinPoint.getArgs();
			/** 参数名*/
			String[] argNames = joinPointObject.getParameterNames();
			/** 获取参数类型*/
			Class[] paramTypes = joinPointObject.getParameterTypes();
			Map<String, Object> map = new HashMap();
			for (int i = 0; i < argNames.length; i++) {
    
    
				if (!paramTypes[i].getName().equals("javax.servlet.http.HttpServletRequest") && !paramTypes[i].getName()
						.equals("javax.servlet.http.HttpServletResponse") && !paramTypes[i].getName()
						.equals("org.springframework.web.multipart.MultipartFile") && !paramTypes[i].getName()
						.equals("org.apache.catalina.servlet4preview.http.HttpServletRequest")) {
    
    
					if (args[i] instanceof Object) {
    
    
						try {
    
    
							map = JSON.parseObject(JSON.toJSONString(args[i]), Map.class);
						} catch (Exception e) {
    
    
							map.put(argNames[i], args[i]);
						}
						map.remove("key");
						map.remove("newKey");
					}
				}
			}
			/** 转换成json*/
			JSONObject json = (JSONObject) JSONObject.toJSON(map);
			screenParam("[前置输出VO]", json, joinPointObject.getDeclaringTypeName(), joinPointObject.getName(), "参数");
		}
	}

	@AfterReturning(value = "pointCut()", returning = "keys")//后置通知
	public void After(JoinPoint joinPoint, Object keys) throws Exception {
    
    
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletResponse response = attributes.getResponse();
		// 获取切入的 Method
		MethodSignature joinPointObject = (MethodSignature) joinPoint.getSignature();
		JSONObject json = (JSONObject) JSONObject.toJSON(keys);
		screenParam("[后置输出VO]", json, joinPointObject.getDeclaringTypeName(), joinPointObject.getName(), "返回值");
	}

	/**
	 * 筛选数据,不符合规则的不打印
	 */
	public void screenParam(String position, JSONObject jsonObject, String typeName, String methodName,
			String paramsName) throws Exception {
    
    
		MySlf4j.textInfo("{0}路径:{1},方法名:{2},{3}:{4}", position, typeName, methodName, paramsName,
				JSON.toJSONString(jsonObject));
	}
}

Supongo que te gusta

Origin blog.csdn.net/python_mopagunda/article/details/128797997
Recomendado
Clasificación