java-aop之日志应用

xml配置-开启aop监听

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.suning.logistics.ldcs.report.aop" />
    <!-- 开启AOP监听 只对当前配置文件有效 -->
    <aop:aspectj-autoproxy expose-proxy="true"/>
</beans>

log定义接口

import java.lang.annotation.*;

/**
 * report系统RSF接口全局日志注解
 *
 * @author 
 * date 
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ReportLog {
    String logPrefix() default "";
}

参数说明

取值(ElementType)有:

    1.CONSTRUCTOR:用于描述构造器
    2.FIELD:用于描述域
    3.LOCAL_VARIABLE:用于描述局部变量
    4.METHOD:用于描述方法
    5.PACKAGE:用于描述包
    6.PARAMETER:用于描述参数
    7.TYPE:用于描述类、接口(包括注解类型) 或enum声明

取值(Retention)有:

1、RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
2、RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
3、RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;
实现类

/**
 * RSF接口入参打印和全局异常捕获,只对使用注解{@link ReportLog}的方法有效。
 * 使用此注解的方法可以不用对具体的异常进行捕获。在方法抛出异常时,如果方法的
 * 返回类型为{@link Response},会返回一个带有“服务器异常”提示的{@link Response}对象。
 * 建议使用{@link Response}或者{@link java.util.Map}作为方法的返回类型
 *
 * @author
 * date 
 */
@Component
@Aspect
public class ReportLogAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReportLogAspect.class);

    @Around("@annotation(com.suning.logistics.ldcs.report.annotation.ReportLog)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = pjp.getTarget().getClass().getMethod(signature.getName(), signature.getParameterTypes());
        ReportLog reportLog = method.getAnnotation(ReportLog.class);
        String logPrefix = reportLog.logPrefix();
        Object[] args = pjp.getArgs();
        StringBuilder methodArgsString = new StringBuilder();
        methodArgsString.append("[");
        for (int i = 0; i < args.length; i++) {
            methodArgsString.append("入参").append(i).append(":").append(null == args[i] ? LDCSConstants.STR_EMPTY : args[i].toString());
        }
        methodArgsString.append("]");
        //方法入参日志打印
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(logPrefix + methodArgsString.toString());
        }
        try {
            return pjp.proceed();
        } catch (Exception e) {
            LOGGER.error(logPrefix + "[异常{}]", e);
            Class<?> returnType = method.getReturnType();
            //防止产生ClassCastException
            if (Response.class.equals(returnType)) {
                return Response.fail(LDCSConstants.SERVICE_ERROR);
            }
            return returnType.newInstance();
        }
    }
}

具体使用-通过注解

   @ReportLog(logPrefix = "GPS_ERROR表分页查询:")
    public Response<PageBean> queryGpsErrorPage(Map<String, Object> params) {
        return Response.success(gpsErrorService.queryByPage(params));
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值