java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Advice通知实例

SpringAop中的Advice通知实例

作者:至学者

这篇文章主要介绍了SpringAop中的Advice通知详解,Spring的AOP功能中一个关键概念是通知Advice与切点Pointcut表达式相关联在特定节点织入一些逻辑,Spring提供了五种类型的通知,需要的朋友可以参考下

一 概述

二 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实例了!

到此这篇关于SpringAop中的Advice通知详解的文章就介绍到这了,更多相关Advice通知内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文