AOP

spring boot aop log拦截配置

星期一, 五月 25th, 2020 | JAVA-and-J2EE | 没有评论

要记录web的入参和出参及方法执行情况,执行如下配置即可

package com.pomelolee.configuration;
 
import lombok.extern.slf4j.Slf4j;
 
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.multipart.MultipartFile;
 
@Slf4j
@Aspect
@Component
public class WebControllerLogConfig {
 
    @Pointcut("execution(public * com.pomelolee.endpoint.*.*(..))")
    public void webRecordLog() {
 
    }
 
    @Around("webRecordLog()")
    public Object process(ProceedingJoinPoint jp) throws Throwable {
        String className = jp.getSignature().getDeclaringTypeName();
        String methodName = ((MethodSignature) jp.getSignature()).getMethod().getName();
        String classMethod = className + "." + methodName;
        Object[] arguments = jp.getArgs();
        Object[] args = new Object[arguments.length];
        for(int i=0;i<arguments.length;i++) {
    	 if (arguments[i] instanceof ServletRequest || arguments[i] instanceof ServletResponse || arguments[i] instanceof MultipartFile) { 
             //ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
             //ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
               continue;
           }
           args[i] = arguments[i];
        }
        log.info("===controller classMethod:{},input args:{} ====", classMethod, JsonKit.toJSONString(args));
        final StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object result = jp.proceed();
        stopWatch.stop();
        final String logMessage = StringUtils.leftPad(Long.toString(stopWatch.getTotalTimeMillis()), 5) + " ms ";
        log.info("the classMethod: {} cost time: {}",classMethod,logMessage);
        log.info("===controller  classMethod: {} ,input args:{} ====", classMethod, JsonKit.toJSONString(result));
        return result;
    }
}

比较完整,排除了 out等输出的异常情况

Tags: ,

spring的设计模式,IOC,AOP的认识 基于2.X

星期日, 九月 21st, 2008 | JAVA-and-J2EE | 一条评论

Spring体系结构

    Spring核心模块实现了IoC的功能,BeanFactory接口是Spring框架的核心接口 —使之Spring 成为一个Bean的容器
    Application Context模块,扩展核心模块,添加了il8n国际化、Bean生命周期控制、框架事件体系、资源加载透明化等功能,且提供了邮件服务、任务调度、JNDI定位、EJB集成、远程访问等企业应用功能。
    AOP模块 —Spring实现了AOP Alliance规范的实现,且整合了AspectJ的AOp语言级的框架(元数据及动态代理的实现)
    Spring DAO模块—-提供检查型的异常转换成非检查型异常,且提供声明式事务支持(事务、DAO、JDBC)

› Continue reading

Tags: , , ,

Search

文章分类

Links

Meta