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的主要内容 随着软件结构的日益庞大,软件模块化趋势出现,软件开发也需要多人合 ...
随机推荐
- 寻找总和为n的连续子数列之算法分析
看到有这么道算法题在博客园讨论,算法eaglet和邀月都已经设计出来了,花了点时间读了下,学到点东西顺便记录下来吧. 题目是从1...n的数列中,找出总和为n的连续子数列. 这里先设好算法中需要用到的 ...
- POJ2080:Calendar(计算日期)
Calendar Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12842 Accepted: 4641 Descrip ...
- HeartBleed bug
前两年的一个严重漏洞,影响很大.出现在openssl 1.0.1和1.0.2 beta(包含1.0.1f和1.0.2beta1).利用了TLS的heartbeat. 简单的说,该漏洞被归为缓冲过度读取 ...
- 第 七 课 go的运算符
http://www.runoob.com/go/go-operators.html 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 返回变量存储地址: & 指针变量: ...
- 通过DBCC整理Sqlserver数据库表索引碎片
昨天检查了一张效率极慢的表,两年多没有维护,逻辑扫描碎片高达99.%,于是开始对这个表进行重点跟踪和记录日志.今天用DBCC SHOWCONTIG WITH TABLERESULTS 命令检查了一下所 ...
- Ajax前端调后台方法
后台对当前页面类进行注册 Ajax.Utility.RegisterTypeForAjax(typeof(Login));//Login 当前类名 在方法上面加 [Ajax.AjaxMethod(Aj ...
- 3.JasperReports学习笔记3-在浏览器生成PDF文件
转自:https://i.cnblogs.com/posts?categoryid=921197 一.新建web工程,导入jasperreports所需的jar包,配置web.xml <serv ...
- EchoServer和EchoClient模型的改进1之多线程
在之前的EchoServer模型个EchoClient模型中,客户端和服务端只是单纯的一一对应的关系,如果存在多个客户端和一个服务端,这就需要具体处理了.在这里我们明显想到的第一种方案是使用多线程处理 ...
- uva 10934 Dropping water balloons
你有k个一模一样的水球,在一个n层楼的建筑物上进行测试,你想知道水球最低从几层楼往下丢可以让水球破掉.由于你很懒,所以你想要丢最少次水球来测出水球刚好破掉的最低楼层.(在最糟情况下,水球在顶楼也不会破 ...
- 树莓派 Learning 002 装机后的必要操作 --- 07 设置静态IP地址
树莓派 装机后的必要操作 之 设置静态IP地址 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 为了避免IP变来变去,我们将IP地址设置为静 ...