`

spring aop日志拦截

 
阅读更多
1.日志拦截类

package com.zebra.carcloud.trip.aspect;  

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
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.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.zebra.carcloud.trip.annotation.ParamAnonymous;
import com.zebra.carcloud.trip.utils.ReqIdUtil;
import com.zebra.carcloud.trip.utils.StringUtil;


@Aspect
@Component
@Order(100)
public class LogAspect {
	static Logger  logger = LoggerFactory.getLogger(LogAspect.class);
	@Pointcut("execution(* com.zebra.carcloud.trip.service.*.*.*(..))")  
	public void executeService(){  
	} 
    @Before("executeService()")
    public void logBefore(JoinPoint joinPoint) {
    	try {
        	if(ReqIdUtil.getReqId() == null) {
        		ReqIdUtil.setReqId(ReqIdUtil.getReqIdForService());
        	}
        	StringBuilder startLog = new StringBuilder(joinPoint.getTarget().getClass().getName())
            .append(" ****").append(joinPoint.getSignature().getName()).append(" 方法执行开始...****   REQ_ID:").append(ReqIdUtil.getReqId()).append("  参数为:");
        	
        	//获取拦截接口方法
        	MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();  
            Method method = methodSignature.getMethod();
            String[] anonymousParams = null;
            if (method.isAnnotationPresent(ParamAnonymous.class)) {
            	ParamAnonymous data = method.getAnnotation(ParamAnonymous.class);
            	anonymousParams = data.params();
            }
            String[] paramName = getFieldsName(this.getClass(),joinPoint.getTarget().getClass().getName(),methodSignature.getMethod().getName());
            Map<String,Object> paramMap = new HashMap<String,Object>();
            int idx = 0;
            Object[] values = joinPoint.getArgs();
            for(String name : paramName) {
            	if(anonymousParams == null || Arrays.binarySearch(anonymousParams, name) < 0) {
            		paramMap.put(name,values[idx++]);
            	}
            }
        	logger.info(startLog.append(StringUtil.getJsonByObj(paramMap)).toString());
        	startLog = null;
    	} catch(Exception e) {
    		
    	}

    }
    @After("executeService()")
    public void logAfter(JoinPoint joinPoint) {
    	logger.info(new StringBuilder(joinPoint.getTarget().getClass().getName())
        .append(" ****").append(joinPoint.getSignature().getName()).append(" 方法执行结束...****   REQ_ID:").append(ReqIdUtil.getReqId()).toString());
    }
    /** 
     * 得到方法参数的名称 
     * @throws NotFoundException 
     */  
    private static String[] getFieldsName(Class<?> cls, String clazzName, String methodName) throws NotFoundException{  
        ClassPool pool = ClassPool.getDefault();  
        ClassClassPath classPath = new ClassClassPath(cls);  
        pool.insertClassPath(classPath);  
          
        CtClass cc = pool.get(clazzName);  
        CtMethod cm = cc.getDeclaredMethod(methodName);  
        MethodInfo methodInfo = cm.getMethodInfo();  
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();  
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);  
        String[] paramNames = new String[cm.getParameterTypes().length];  
        int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;  
        for (int i = 0; i < paramNames.length; i++){  
            paramNames[i] = attr.variableName(i + pos); 
        }  
        return paramNames;  
    }  
}
 


2.
分享到:
评论

相关推荐

    spring aop切面拦截指定类和方法实现流程日志跟踪

    本节将详细介绍如何使用Spring AOP实现流程日志跟踪,主要关注于如何通过AOP拦截特定的类和方法来进行日志记录。 ##### 3.1 配置Spring AOP 在Spring配置文件中定义切面和切入点表达式是非常关键的一步。一般来说...

    springboot+aspect实现springaop拦截指定方法.zip

    SpringBoot结合AspectJ实现SpringAOP拦截指定方法的知识点涵盖了多个方面,这包括Spring AOP的基本概念、SpringBoot的应用、切点(Pointcut)与通知(Advice)的定义、自定义注解以及AspectJ的使用。以下是这些知识...

    Spring Mvc AOP通过注解方式拦截controller等实现日志管理

    Spring MVC AOP日志管理通常与日志框架(如Log4j、Logback或Java内置的java.util.logging)集成。以下以Log4j为例: 1. 引入依赖:在项目pom.xml中添加Log4j的依赖。 2. 配置Log4j:创建log4j.properties或log4j....

    springaop拦截controller日志

    "springaop拦截controller日志"这个主题旨在讲解如何使用Spring AOP来拦截Controller层的方法调用,并在方法执行前后记录相关日志。 首先,了解Spring AOP的基本概念。AOP是一种编程范式,它允许程序员定义“切面”...

    SpringAOP日志管理

    采用SpringAOP拦截Controller,Service实现操作日志管理,统一处理异常,登陆日志管理,是SpringAOP的应用实践。通过SpringAOP的处理,可以方便移植日志管理功能,是个不错的学习demo

    ssh2登陆+spring aop做拦截

    SSH2 登录与 Spring AOP 拦截是两种在 IT 领域中常见的技术,主要用于提升系统安全性和管理效率。SSH2(Secure Shell version 2)是一种网络协议,用于提供安全的远程登录和数据传输,而 Spring AOP(Aspect ...

    springboot spring aop 拦截器注解方式实现脱敏

    在Spring Boot应用中,Spring AOP(面向切面编程)是一种强大的工具,它允许我们创建横切关注点,如日志记录、权限检查等,这些关注点可以被编织到应用程序的多个点上,而无需侵入核心业务逻辑。在本案例中,我们将...

    spring aop 拦截日志示例

    在Spring AOP(面向切面编程)中,我们可以通过定义拦截器来实现对系统操作日志和异常日志的记录,这些日志信息通常会被存储到数据库中以便于后续的分析和故障排查。下面将详细介绍如何使用Spring AOP实现这个功能。...

    SpringAOP的日志拦截示例

    本示例将详细解析如何在Spring AOP中实现日志拦截。 首先,我们需要创建一个拦截器类,这个类通常会实现Spring的`MethodBeforeAdvice`、`AfterReturningAdvice`或`ThrowsAdvice`接口,或者自定义一个Advisor。在这...

    ssh+aop+log4j+日志拦截器+注解

    标题中的"ssh+aop+log4j+日志拦截器+注解"涉及到的是Java Web开发中的几个核心组件和技术,这些技术在构建大型、分布式的企业级应用时常常被使用。下面将详细介绍这些知识点: 1. SSH (Spring, Struts, Hibernate)...

    在自定义spring aop中使用el获取拦截方法的变量值。

    Spring AOP是Spring框架的一个重要特性,它允许开发者创建具有横切关注点的模块,如日志记录、事务管理等。这些关注点可以被声明性地应用到多个对象上,而无需修改这些对象的代码。Spring AOP通过动态代理实现,有两...

    简单spring aop 例子

    Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种模块化和声明式的方式来处理系统中的交叉关注点问题,如日志、事务管理、安全性等。本示例将简要介绍如何在Spring应用中实现AOP,通过实际的...

    spring aop 拦截实例

    在"spring aop 拦截实例"中,我们通常会看到以下步骤: 1. **配置AOP**:在Spring配置文件中启用AOP并注册切面类。 2. **定义切面**:创建一个Java类,使用`@Aspect`注解,并定义切入点表达式和通知方法。 3. **...

    springMVC AOP拦截拦截Controller等实现日志管理

    在Spring MVC中,AOP(面向切面编程)是一种强大的工具,可以让我们在不修改代码的情况下,对程序的特定部分进行增强,例如日志记录、事务管理或性能监控。在这个场景中,我们将讨论如何利用AOP来实现Controller的...

    Spring使用AOP的三个jar包

    它定义了一些通用的AOP接口,比如`org.aopalliance.intercept.MethodInterceptor`和`org.aopalliance.intercept.MethodInvocation`,使得不同的AOP框架(如Spring和AspectJ)可以共享相同的拦截器(interceptors)。...

    spring aop 拦截器简单实现

    在Spring AOP中,拦截器扮演着关键角色。它是一个实现了`org.springframework.aop.MethodBeforeAdvice`、`org.springframework.aop.AfterReturningAdvice`或`org.springframework.aop.ThrowsAdvice`等接口的对象,...

    spring AOP拦截方法小示例

    在Spring框架中,AOP(面向切面编程)是一种强大的设计模式,用于处理系统中的横切关注点,如日志记录、事务管理、性能监控等。这个“spring AOP拦截方法小示例”是一个实际应用,展示了如何使用Spring AOP来拦截...

    Spring Aop四个依赖的Jar包

    Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的一个重要模块,它通过提供声明式的方式来实现面向切面编程,从而简化了应用程序的开发和维护。在Spring AOP中,我们无需深入到每个...

    详解Spring AOP 拦截器的基本实现

    Spring AOP使用代理模式和动态代理技术来拦截方法调用,根据切点表达式匹配的方法会触发定义的通知。 权限管理、事务管理、安全管理、日志管理和调试管理是AOP在实际项目中常见的应用场景。这些通常不需要与业务...

Global site tag (gtag.js) - Google Analytics