LogAspect
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
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.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import com.yd.common.runtime.CIPRuntime;
import com.yd.common.runtime.CIPRuntimeConstants;
import com.yd.common.session.CIPHttpSession;
import com.yd.common.session.CIPSessionManager;
import com.yd.common.session.CIPUser; /**
* 第三步:声明一个切面
*
* 记录日记的切面
*
* @author ZRD
*
*/
@Component
@Aspect
public class LogAspect { private final Logger logger = Logger.getLogger(getClass());
/**
* 切入点:表示在哪个类的哪个方法进行切入。配置有切入点表达式
*/ /*@Pointcut("execution(* com.yd..*.*(..))")
public void pointcutExpression() { }*/ /**
* 1 前置通知
* @param joinPoint
*/
// @Before("execution(* com.yd.wmsc.busi.service.local.LWMSC_busi_inboundServiceImpl.*(..))")
//@Before("execution(* com.yd.wmsc.busi.service..*(..))")
//@Before("execution(* com.yd.wmsc.*.service..*(..))")
//@Before("execution(* com.yd..service..*(..))")
//@Before("execution(* *..service..*(..))")//service包下的所有方法
//@Before("execution(* *..*Service*..*(..))")//带有Service的类的所有方法
public void beforeMethod(JoinPoint joinPoint) {
System.out.println("前置通知执行了");
}
/**
* 2 后置通知
*/ public void afterMethod(JoinPoint joinPoint) {
System.out.println("后置通知执行了,有异常也会执行");
}
/**
* 3 返回通知
*
* 在方法法正常结束受执行的代码
* 返回通知是可以访问到方法的返回值的!
*
* @param joinPoint
* @param returnValue
*
*/
/* @AfterReturning(value = "pointcutExpression()", returning = "returnValue")
public void afterRunningMethod(JoinPoint joinPoint, Object returnValue) {
System.out.println("返回通知执行,执行结果:" + returnValue);
}*/ /**
* 4 异常通知
*
* 在目标方法出现异常时会执行的代码.
* 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码
*
* @param joinPoint
* @param e
*/
/*@AfterThrowing(value = "pointcutExpression()", throwing = "e")
public void afterThrowingMethod(JoinPoint joinPoint, Exception e){
System.out.println("异常通知, 出现异常 :" + e);
}*/ /**
* 环绕通知需要携带 ProceedingJoinPoint 类型的参数.
* 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.
* 且环绕通知必须有返回值, 返回值即为目标方法的返回值
*/ /*@Around("pointcutExpression()")
public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null;
String methodName = pjd.getSignature().getName(); try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
//执行目标方法
result = pjd.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
//异常通知
System.out.println("The method " + methodName + " occurs exception:" + e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method " + methodName + " ends"); return result;
}*/ @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object aroundMethod(ProceedingJoinPoint pjd){ String requestPath = null; // 请求地址
String userId = null;
String userName = null; // 用户名
//Map<?, ?> inputParamMap = null; // 传入参数
//Map<String, Object> outputParamMap = null; // 存放输出结果 Object result = null;
Object args[] = pjd.getArgs();
MethodSignature signature = (MethodSignature) pjd.getSignature();
Method method = signature.getMethod();
long startTimeMillis = System.currentTimeMillis();
try {
/**
* 1.获取request信息
* 2.根据request获取session
* 3.从session中取出登录用户信息 */
RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes)ra;
HttpServletRequest request = sra.getRequest();
if (CIPRuntime.getOperateSubject()!=null ) {
userId =CIPRuntime.getOperateSubject().getSubject_id();
userName =CIPRuntime.getOperateSubject().getSubject_name();
} // 从session中获取用户信息
// userName = (String) session.getAttribute("userName");
//String sessionId = runtimeInfo.get(CIPRuntimeConstants.SYSTEM_SESSION_ID);
//CIPHttpSession systemSession = CIPSessionManager.getSession(request, response);
//CIPUser systemUser = systemSession.getAttribute(CIPRuntimeConstants.LOGIN_USER, CIPUser.class);
// 获取输入参数
//inputParamMap = request.getParameterMap();
// 获取请求地址
requestPath = request.getRequestURI(); // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
//outputParamMap = new HashMap<String, Object>(); //执行目标方法
result = pjd.proceed();
//outputParamMap.put("result", result);
} catch (Throwable e) {
//异常通知
logger.error(e);
throw new RuntimeException(e);
}
long endTimeMillis = System.currentTimeMillis();
String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);
//后置通知 logger.debug("\n userId:"+userId +" userName:"+userName +" url:"+requestPath+"; op_time:" + optTime + " pro_time:" + (endTimeMillis - startTimeMillis) + "ms ;"
+"\n"+ method.getDeclaringClass().getName()+"."+ method.getName()+":"+ Arrays.toString(args)+"\n"+"result:"+result); return result;
}
}
LogAspect的更多相关文章
- 接口日志记录AOP实现-LogAspect
使用spring aop日志记录 所需jar包 pom.xml <!-- logger begin --> <dependency> <groupId>org.sl ...
- java logAspect
@Around("execution(* com.iotx.cep.biz.rpc.impl.*.*(..)) " + "&& !execution(* ...
- 利用AOP与ToStringBuilder简化日志记录
刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...
- Java Spring的IoC和AOP的知识点速记
Spring简介 Spring解决的最核心的问题就是把对象之间的依赖关系转为用配置文件来管理,这个是通过Spring的依赖注入机制实现的. Spring Bean装配 1. IOC的概念以及在Spri ...
- Spring中AOP(通知)的使用
1.新建 Spring Bean Configuration File xml格式的文件 2. xml文件 <bean id="my1" class="xml.M ...
- Spring(三)AOP面向切面编程
原文链接:http://www.orlion.ga/205/ 一.AOP简介 1.AOP概念 参考文章:http://www.orlion.ml/57 2.AOP的产生 对于如下方法: pub ...
- 使用AOP+Annotation实现操作日志记录
先创建注解 OperInfo @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @ ...
- Spring-AOP面向切面编程
AOP是面向切面编程,区别于oop,面向对象,一个是横向的,一个是纵向. 主要解决代码分散和混乱的问题. 1.概念: 切面:实现AOP共有的类 通知:切面类中实现切面功能的方法 连接点:程序被通知的特 ...
- Spring 000 框架简介 (转载)
转载自:https://my.oschina.net/myriads/blog/37922 1.使用框架的意义与Spring的主要内容 随着软件结构的日益庞大,软件模块化趋势出现,软件开发也需要多人合 ...
随机推荐
- POJ1041 John's trip
John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- 51nod 1686 第K大区间2
1685 第K大区间2 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. 众数(统计学/数学名词)_百度百科 Input 第一行两个数n和k(1<=n ...
- WSGI服务与django的关系
WSGI接口 wsgi是将python服务器程序连接到web服务器的通用协议.uwsgi是独立的实现了wsgi协议的服务器. web服务器 服务端程序 简化版的WSGI架构 服务端程序(类似dja ...
- JavaScript RegExp 正则表达式基础详谈
前言: 正则对于一个码农来说是最基础的了,而且在博客园中,发表关于讲解正则表达式的技术文章,更是数不胜数,各有各的优点,但是就是这种很基础的东西,如果我们不去真正仔细研究.学习.掌握,而是抱着需要的时 ...
- Poj1163 The Triangle(动态规划求最大权值的路径)
一.Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a pro ...
- python的WeakKeyDictionary类和weakref模块的其他函数
python的WeakKeyDictionary类和weakref模块的其他函数 # -*- coding: utf-8 -*- # @Author : ydf # @Time : 2019/6/13 ...
- sharepoint Foundation 2013安装过程
安装完必备软件后,便可安装sharepoint Foundation 2013
- EPEL for CentOS or Redhat
注:地址可能会变 RHEL/CentOS 7 64 Bit # wget http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release ...
- Luogu 4317 花神的数论题
披着数论题外衣的数位dp. 相当于数一数$[1,n]$范围内$1$的个数是$1,2,3,4,...log(n)$的数各有多少个,直接在二进制下数位dp. 然而我比较sb地把(1e7 + 7)当成了质数 ...
- Git for Windows,TortoiseGit支持WinXP的最后版本及下载方法
TortoiseGit兼容Windows XP和Windows Server 2003的最后版本: TortoiseGit 1.8.16.0 (https://download.tortoisegit ...