- 浏览: 265399 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (298)
- 工作感悟 (6)
- java基础 (23)
- 计算机硬件知识 (1)
- 计算机网络知识 (2)
- Struts (3)
- Srping (4)
- hibernate (0)
- ibatis (0)
- webservice (4)
- Thread (22)
- maven (5)
- ubuntu/linux/centos/redhat (46)
- SSO (1)
- ESB (0)
- 工作流 (0)
- oracle (15)
- 云计算 (1)
- hadoop (1)
- nosql (0)
- mysql (3)
- sqlserver (0)
- jquery (0)
- 分布式 (3)
- 集群 (0)
- 设计模式 (2)
- EJB (0)
- map (0)
- cache (5)
- Niginx+varnish+squid+Ats (14)
- Apache (0)
- 工作/职业规划 (0)
- Scala & Groovy (1)
- English (4)
- 数据结构/算法 (6)
- 开发工具 (5)
- 测试 (2)
- Exception (0)
- 定时器 (3)
- j2ee (2)
- 部署 (1)
- Openssl (1)
- 操作系统 (3)
- kvm (13)
- libvirt (5)
- PostgreSql (5)
- 虚拟化 (3)
- 概念理解 (1)
- virt-manager (1)
- RESTful (3)
- 其它 (4)
- ssh2 (14)
- windows (1)
- 房产 (2)
- svn (1)
- 手机 (1)
- ant (1)
- flume (2)
- sqoop (1)
- fastdfs (5)
- log4j (1)
- SPDY (1)
- mongodb (2)
- MQ (2)
- Mina (1)
- dubbo (4)
- PMP (1)
- Webshpere (2)
- jvm (1)
- Btrace (1)
- zookeeper (7)
- UML (1)
- spring cloud (6)
- spring boot (5)
- storm (0)
- 软件管理 (1)
- elasticsearch (1)
- 协议 (2)
- docker (1)
- 性能 (2)
- 安全 (1)
- 代码规范 (1)
- mqtt (1)
- lombok (1)
- 车联网 (1)
- kafka (1)
最新评论
1.日志拦截类
2.
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实现流程日志跟踪,主要关注于如何通过AOP拦截特定的类和方法来进行日志记录。 ##### 3.1 配置Spring AOP 在Spring配置文件中定义切面和切入点表达式是非常关键的一步。一般来说...
SpringBoot结合AspectJ实现SpringAOP拦截指定方法的知识点涵盖了多个方面,这包括Spring AOP的基本概念、SpringBoot的应用、切点(Pointcut)与通知(Advice)的定义、自定义注解以及AspectJ的使用。以下是这些知识...
Spring MVC AOP日志管理通常与日志框架(如Log4j、Logback或Java内置的java.util.logging)集成。以下以Log4j为例: 1. 引入依赖:在项目pom.xml中添加Log4j的依赖。 2. 配置Log4j:创建log4j.properties或log4j....
"springaop拦截controller日志"这个主题旨在讲解如何使用Spring AOP来拦截Controller层的方法调用,并在方法执行前后记录相关日志。 首先,了解Spring AOP的基本概念。AOP是一种编程范式,它允许程序员定义“切面”...
采用SpringAOP拦截Controller,Service实现操作日志管理,统一处理异常,登陆日志管理,是SpringAOP的应用实践。通过SpringAOP的处理,可以方便移植日志管理功能,是个不错的学习demo
SSH2 登录与 Spring AOP 拦截是两种在 IT 领域中常见的技术,主要用于提升系统安全性和管理效率。SSH2(Secure Shell version 2)是一种网络协议,用于提供安全的远程登录和数据传输,而 Spring AOP(Aspect ...
在Spring Boot应用中,Spring AOP(面向切面编程)是一种强大的工具,它允许我们创建横切关注点,如日志记录、权限检查等,这些关注点可以被编织到应用程序的多个点上,而无需侵入核心业务逻辑。在本案例中,我们将...
在Spring AOP(面向切面编程)中,我们可以通过定义拦截器来实现对系统操作日志和异常日志的记录,这些日志信息通常会被存储到数据库中以便于后续的分析和故障排查。下面将详细介绍如何使用Spring AOP实现这个功能。...
本示例将详细解析如何在Spring AOP中实现日志拦截。 首先,我们需要创建一个拦截器类,这个类通常会实现Spring的`MethodBeforeAdvice`、`AfterReturningAdvice`或`ThrowsAdvice`接口,或者自定义一个Advisor。在这...
标题中的"ssh+aop+log4j+日志拦截器+注解"涉及到的是Java Web开发中的几个核心组件和技术,这些技术在构建大型、分布式的企业级应用时常常被使用。下面将详细介绍这些知识点: 1. SSH (Spring, Struts, Hibernate)...
Spring AOP是Spring框架的一个重要特性,它允许开发者创建具有横切关注点的模块,如日志记录、事务管理等。这些关注点可以被声明性地应用到多个对象上,而无需修改这些对象的代码。Spring AOP通过动态代理实现,有两...
Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种模块化和声明式的方式来处理系统中的交叉关注点问题,如日志、事务管理、安全性等。本示例将简要介绍如何在Spring应用中实现AOP,通过实际的...
在"spring aop 拦截实例"中,我们通常会看到以下步骤: 1. **配置AOP**:在Spring配置文件中启用AOP并注册切面类。 2. **定义切面**:创建一个Java类,使用`@Aspect`注解,并定义切入点表达式和通知方法。 3. **...
在Spring MVC中,AOP(面向切面编程)是一种强大的工具,可以让我们在不修改代码的情况下,对程序的特定部分进行增强,例如日志记录、事务管理或性能监控。在这个场景中,我们将讨论如何利用AOP来实现Controller的...
它定义了一些通用的AOP接口,比如`org.aopalliance.intercept.MethodInterceptor`和`org.aopalliance.intercept.MethodInvocation`,使得不同的AOP框架(如Spring和AspectJ)可以共享相同的拦截器(interceptors)。...
在Spring AOP中,拦截器扮演着关键角色。它是一个实现了`org.springframework.aop.MethodBeforeAdvice`、`org.springframework.aop.AfterReturningAdvice`或`org.springframework.aop.ThrowsAdvice`等接口的对象,...
在Spring框架中,AOP(面向切面编程)是一种强大的设计模式,用于处理系统中的横切关注点,如日志记录、事务管理、性能监控等。这个“spring AOP拦截方法小示例”是一个实际应用,展示了如何使用Spring AOP来拦截...
Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的一个重要模块,它通过提供声明式的方式来实现面向切面编程,从而简化了应用程序的开发和维护。在Spring AOP中,我们无需深入到每个...
Spring AOP使用代理模式和动态代理技术来拦截方法调用,根据切点表达式匹配的方法会触发定义的通知。 权限管理、事务管理、安全管理、日志管理和调试管理是AOP在实际项目中常见的应用场景。这些通常不需要与业务...