回顾SpringAOP中的Advice

 

一 概述

@Before:前置通知

@After :后置通知,方法执行完之后

@AfterReturning:返回通知,完成执行之后

@AfterThrowing:异常通知,抛出异常之后

@Around:环绕通知

二 Advice应用实例

通过环绕通知获取方法执行时间

@Component
@Aspect
public class RepositoryAspect {

    @Pointcut("execution(* com.repository..*(..))")
    private void crud() {
    }

    //环绕通知
    @Around("crud()")
    public Object logPerformance(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTimes = System.currentTimeMillis();
        Object result = null;
        String name = "-";
        try {
            name = joinPoint.getSignature().toShortString();
            result = joinPoint.proceed();
            return result;
        } catch (Throwable t) {
            throw t;
        } finally {
            long duration = System.currentTimeMillis() - startTimes;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(name);
            System.out.println(stringBuilder+"执行时间为"+duration)
        }
    }
}
public class AbstractAspect {
	protected ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

	protected ThreadLocal<Map<String, Object>> getThreadLocal(){
		if (threadLocal.get() == null){
			threadLocal.set(new HashMap<>());
		}
		return threadLocal;
	}
}
@Component
@Aspect
public class ControllerAspect extends AbstractAspect {

    //对于controller包下所有类和方法进行获取,除被注解NoHealth标注的类除外。
    @Pointcut("execution(* com.controller..*(..))&&!@annotation(NoHealth)")
    private void controllerPointCut() {
    }

    //在方法执行之前对参数进行处理 
    @Before(value = "controllerPointCut()")
    public void doBefore(JoinPoint joinPoint) {
        try {
            getThreadLocal().get().put(joinPoint.getSignature().toString(), System.currentTimeMillis());
            Object[] args = joinPoint.getArgs();
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            String[] parameterNamesArray = methodSignature.getParameterNames();
            int i = 0;
            List<String> argsList = new ArrayList<>();
            if (!ObjectUtils.isEmpty(args)) {
                for (Object object : args) {
                    if (ObjectUtils.isEmpty(object)
                            || (object instanceof BeanPropertyBindingResult)
                            || (object instanceof HttpServletResponse)
                            || (object instanceof Boolean)
                            || (object instanceof String)
                            || (object instanceof Integer)) {

                        if ((object instanceof Boolean) || (object instanceof String) || (object instanceof Integer)) {
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append("{\"");
                            stringBuilder.append(parameterNamesArray[i]);
                            stringBuilder.append("\":\"");
                            stringBuilder.append(object);
                            stringBuilder.append("\"}");
                            argsList.add(JSONObject.fromObject(String.valueOf(stringBuilder)).toString());
                        }
                        i++;
                        continue;//跳出当前循环,且不执行此语句后面的语句!
                    }

                    if (!(object instanceof HttpServletRequest)) {
                        argsList.add(JSONObject.fromObject(object).toString());
                    } else {
                        HttpServletRequest request = (HttpServletRequest) object;
                        Enumeration<String> parameterNames = request.getParameterNames();
                        Map<String, String> argsMap = new HashMap<>();
                        while (parameterNames.hasMoreElements()) {
                            String elementName = parameterNames.nextElement();
                            argsMap.put(elementName, request.getAttribute(elementName).toString());
                        }
                        if (!ObjectUtils.isEmpty(argsMap)) {
                            argsList.add(JSONObject.fromObject(argsMap).toString());
                        }
                    }
                    i++;
                }
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(JSONArray.fromObject(argsList));
        } catch (Exception e) {
          
        }
    }

    //对方法执行之后的返回值进行某种操作
    @AfterReturning(value = "controllerPointCut()", returning = "response")
    public void responseLog(JoinPoint joinPoint, Object response) {
        try {
            if (response == null) {
                response = new Object();
            }
            long duration = System.currentTimeMillis() - (Long) threadLocal.get().get(joinPoint.getSignature().toString());
            StringBuilder stringB uilder = new StringBuilder();
            stringBuilder.append(JSONObject.fromObject(response));
        } catch (Exception e) {
           
        }
    }

    //对方法抛出的异常进行操作
    @AfterThrowing(value = "controllerPointCut()", throwing = "e")
    public void exceptionLog(Exception e) {

    }
}

根据这些实例就可以参照写一些简单的SpringAOP实例了!

猜你喜欢

转载自blog.csdn.net/calm_encode/article/details/113613781
今日推荐