AOP和注解的配合使用(封装通用日志处理类)

自定义注解

@Inherited
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {
    String value() default "";
}

定义切面

@Aspect
@Component
@Slf4j
public class LogAop {
    // 定义识别自定义注解的切点
    @Pointcut("@annotation(com.jxy.MyLog)")
    public void logger(){}

    /**
     *
     * @param point
     * @return
     */
    @Around("logger()")
    public Object around(ProceedingJoinPoint point) {
        Object obj = null;
        String ip_port = "";
        String uri = "";
        String value="";
        HttpServletResponse response = null;
        ControllerLogMapper bip2NCLogMapper = null;
        bip2NCLogMapper = AppContext.getBean(ControllerLogMapper.class);
        Signature methodName = point.getSignature();
        String name = point.getSignature().getName();
        log.error(methodName + "....running");
        //Long start = System.currentTimeMillis();
        Object[] argArray = point.getArgs();
        if (argArray != null && argArray.length > 0) {
            for (Object object : argArray) {
                if (object instanceof HttpServletResponse) {
                    response = (HttpServletResponse) object;
                }
            }
        }
        // 目标方法执行
        try {
            Object res = point.proceed(point.getArgs());
            obj = res ;
        } catch (Throwable throwable) {
            obj = "【" + name + "方法异常,异常信息:" + throwable + "】";
            log.error("【环绕异常通知】【" + name + "方法异常,异常信息:" + throwable + "】");
            throw new GlobalException(throwable.getMessage());
        } finally {
            try {
                if (response != null) {
                    PrintWriter writerToBeRead = response.getWriter();
                    CoyoteWriter cw = (CoyoteWriter) writerToBeRead.append("");
                    Class<?> clazz = cw.getClass();
                    Field declaredField = clazz.getDeclaredField("ob");
                    declaredField.setAccessible(true);
                    OutputBuffer ob = (OutputBuffer) declaredField.get(cw);
                    Class<?> classOutputBuffer = ob.getClass();
                    //获取IP和端口 start
                    Class<?> responseClass = response.getClass();
                    Field requestField = responseClass.getDeclaredField("request");
                    requestField.setAccessible(true);
                    HttpServletRequest request = (HttpServletRequest) requestField.get(response);
                    String ip = request.getRemoteAddr();
                    //获取请求地址
                    uri = request.getRequestURI();
                    Field coyoteResponseField = classOutputBuffer.getDeclaredField("coyoteResponse");
                    coyoteResponseField.setAccessible(true);
                    Response coyoteResponse = (Response) coyoteResponseField.get(ob);
                    Class<?> coyoteResponseClass = coyoteResponse.getClass();
                    Field reqField = coyoteResponseClass.getDeclaredField("req");
                    reqField.setAccessible(true);
                    Request request1 = (Request) reqField.get(coyoteResponse);
                    int port = request1.getServerPort();
                    ip_port = ip + ":" + port;
                    //获取IP和端口 end
                    Field fieldOutputChunk = classOutputBuffer.getDeclaredField("cb");
                    fieldOutputChunk.setAccessible(true);
                    obj = fieldOutputChunk.get(ob) == null ? "" : fieldOutputChunk.get(ob).toString();


                }
                MethodSignature signature = (MethodSignature) point.getSignature();
                MyLog declaredAnnotation = signature.getMethod().getDeclaredAnnotation(MyLog.class);
                value = declaredAnnotation.value();
                log.error("==注解@MyLog的value=="+ value);
                //记录到日志表
                String loginUser = AppContext.getCurrentUser() == null ? "第三方调用" : AppContext.getCurrentUser().getId().toString();
                String YTenantId = com.yonyou.eforship.common.utils.yms.AppContextUtil.getCurrentOrDefaultTenantId() == null ? "" : com.yonyou.eforship.common.utils.yms.AppContextUtil.getCurrentOrDefaultTenantId().toString();
                bip2NCLogMapper.add(String.valueOf(IdManager.getInstance().nextId()), loginUser, ArrayUtils.toString(argArray, ","), obj == null?"":obj.toString(), methodName.toString(), YTenantId, ip_port,uri,value);
            } catch (Exception e) {
                log.error("ControllerLog接口日志创建异常:" + e.getMessage());
            }
            log.error("【环绕后置通知】【" + name + "方法结束】");
        }
        //Long end = System.currentTimeMillis();
        //log.info(methodName + "....stop" + "\t耗时:" + (end - start));
        return obj;
    }

    public String getResponseContent(HttpServletResponse response) throws IOException, NoSuchFieldException, IllegalAccessException {
        String responseContent = null;
        CoyoteOutputStream outputStream = (CoyoteOutputStream) response.getOutputStream();
        Class<CoyoteOutputStream> coyoteOutputStreamClass = CoyoteOutputStream.class;
        Field obField = coyoteOutputStreamClass.getDeclaredField("ob");
        if (obField.getType().toString().endsWith("OutputBuffer")) {
            obField.setAccessible(true);
            org.apache.catalina.connector.OutputBuffer outputBuffer = (org.apache.catalina.connector.OutputBuffer) obField.get(outputStream);
            Class<org.apache.catalina.connector.OutputBuffer> opb = org.apache.catalina.connector.OutputBuffer.class;
            Field outputChunkField = opb.getDeclaredField("outputChunk");
            outputChunkField.setAccessible(true);
            if (outputChunkField.getType().toString().endsWith("ByteChunk")) {
                ByteChunk bc = (ByteChunk) outputChunkField.get(outputBuffer);
                Integer length = bc.getLength();
                if (length == 0) return null;
                responseContent = new String(bc.getBytes(), "UTF-8");
                Integer responseLength = StringUtils.isBlank(responseContent) ? 0 : responseContent.length();
                if (responseLength < length) {
                    responseContent = responseContent.substring(0, responseLength);
                } else {
                    responseContent = responseContent.substring(0, length);
                }

            }
        }
        return responseContent;
    }

    /**
     * 如果程序出现了异常,则需要拦截,打印异常信息
     * @param joinPoint
     * @param throwable
     */
    @AfterThrowing
            (pointcut = "logger()",
                    throwing = "throwable")
    public void afterThrow(JoinPoint joinPoint, Throwable throwable) {
        Class<?> targetClass = joinPoint.getTarget().getClass();
        String methodName = joinPoint.getSignature().getName();
        Class<?> throwClass = throwable.getClass();
        String msg = throwable.getMessage();
        log.error("目标对象类型:" + targetClass);
        log.error("目标方法:" + methodName);
        log.error("异常类型:" + throwClass);
        log.error("异常信息:" + msg);
    }

}

使用

该自定义注解作用于方法上,如果需要日志处理,直接使用注解即可。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于构建独立的、生产级别的Spring应用程序的框架,它提供了丰富的功能和特性,方便开发人员快速搭建和开发项目。 在Spring Boot中使用AOP(面向切面编程)可以实现将一些公共的功能逻辑代码从业务逻辑代码中解耦出来,提高代码的复用性和可维护性。通过AOP可以在方法的前后注入一些通用的逻辑,例如日志记录、异常处理、权限校验等。 其中,使用AOP进行结果的统一封装是很常见的需求。通过AOP可以在方法执行后对返回的结果进行封装,统一处理返回结果的格式,并可以对结果进行一些统一的处理操作,例如添加统一的返回码、返回信息、返回时间等。 在Spring Boot中使用AOP进行结果的统一封装可以按照以下步骤进行: 1. 创建一个切面(Aspect),通过在切面上加上@Aspect注解标识该是一个切面。 2. 在切面中定义一个切点(Pointcut),通过定义一个方法并添加@Pointcut注解来指定切入点。 3. 在切面中定义一个通知(Advice),通过@Before、@After、@Around等注解来指定通知型,并在通知方法中编写需要执行的逻辑。 4. 在通知方法中获取方法的返回结果,并进行相应的封装处理。 5. 在Spring Boot的配置中添加@EnableAspectJAutoProxy注解来启用AOP使用以上步骤可以实现对方法返回结果的统一封装,使得返回结果具有统一的格式和处理逻辑。这样可以提高代码的重用性和可维护性,并且可以在一处对结果进行集中处理,减少了代码的重复性,提高了开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值