@PrintParameter

package com.meng.d_common.annotation;

import java.lang.annotation.*;


/**
 * 打印参数
 *
 * @author MENG
 * @version 2017/7/13
 * @see
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PrintParameter
{
    /**
     * 函数描述
     *
     * @return
     */
    String description()  default "";
}

解析类AOP


package com.meng.d_common.annotation;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
 * 注解RemoveEmptyOrNullParamter aop
 *
 * @author MENG
 * @version 2017/7/13
 * @see
 */
@Aspect
@Component
public class PrintParameterAspect
{
    private static Logger logger = LoggerFactory.getLogger(PrintParameterAspect.class);

    /**
     * 切入点
     */
    @Pointcut("@annotation(com.meng.d_common.annotation.PrintParameter)")
    public void pointCutMethod()
    {
    }

    /**
     * 方法执行之前
     *
     * @param joinPoint 参数
     */
    @Before("pointCutMethod()&&@annotation(printParameter)")
    public void before(JoinPoint joinPoint, PrintParameter printParameter)
    {
        //参数值
        Object[] objects = joinPoint.getArgs();

        Signature signature = joinPoint.getSignature();

        MethodSignature methodSignature = (MethodSignature) signature;

        //参数名
        String[] parameterNames = methodSignature.getParameterNames();

        //类位置
        String className = joinPoint.getTarget().getClass().getName();

        //函数名称
        String methodName = joinPoint.getSignature().getName();

        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();


        logger.info("<==========================================================================================================");

        logger.info("请求接口 : " + className + "-" + methodName + "("+printParameter.description() +")");

        //打印参数
        for (int i = 0; i < objects.length; i++)
        {
            Object obj = objects[i];

            if (obj != null)
            {
                logger.info("请求参数 : " + parameterNames[i] + " : "+ gson.toJson(obj));
            }
            else
            {
                logger.info("请求参数 : " + parameterNames[i] + " : null");
            }
        }
        logger.info("<==========================================================================================================");

    }

    @AfterReturning(returning="rvt", pointcut="pointCutMethod()&&@annotation(printParameter)")
    public void after(JoinPoint joinPoint, Object rvt, PrintParameter printParameter)
    {
        //类位置
        String className = joinPoint.getTarget().getClass().getName();

        //函数名称
        String methodName = joinPoint.getSignature().getName();

        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();

        logger.info("<==========================================================================================================");

        logger.info("返回接口 : " + className + "-" + methodName + "("+printParameter.description() +")");

        if (rvt != null)
        {
            logger.info("返回数据 : " + gson.toJson(rvt));
        }
        else
        {
            logger.info("返回数据 : null");
        }

        logger.info("<==========================================================================================================");
    }

}


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文是我自己在秋招复习时的读书笔记,整理的知识点,也是为了防止忘记,尊重劳动成果,转载注明出处哦!如果你也喜欢,那...
    波波波先森阅读 14,220评论 6 86
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,529评论 11 349
  • SpringAOP 博客链接 本文主要是解析Spring AOP的运作流程。上次讲到Java中的两种动态代理技术:...
    spilledyear阅读 5,673评论 2 32
  • ioc实现原理 转载至:http://jiwenke.iteye.com/blog/493965 IOC基础 下面...
    onlyHalfSoul阅读 4,241评论 1 1
  • 对日本印象最深的一幕,就是走在日本的大街上,就像来到中国的一个城市而已,因为建筑实在与我们国家的古代建筑太像了;目...
    badmask阅读 3,558评论 0 0

友情链接更多精彩内容