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的更多相关文章

  1. 接口日志记录AOP实现-LogAspect

    使用spring aop日志记录 所需jar包 pom.xml <!-- logger begin --> <dependency> <groupId>org.sl ...

  2. java logAspect

    @Around("execution(* com.iotx.cep.biz.rpc.impl.*.*(..)) " + "&& !execution(* ...

  3. 利用AOP与ToStringBuilder简化日志记录

    刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...

  4. Java Spring的IoC和AOP的知识点速记

    Spring简介 Spring解决的最核心的问题就是把对象之间的依赖关系转为用配置文件来管理,这个是通过Spring的依赖注入机制实现的. Spring Bean装配 1. IOC的概念以及在Spri ...

  5. Spring中AOP(通知)的使用

    1.新建 Spring Bean Configuration File  xml格式的文件 2. xml文件 <bean id="my1" class="xml.M ...

  6. Spring(三)AOP面向切面编程

    原文链接:http://www.orlion.ga/205/ 一.AOP简介 1.AOP概念 参考文章:http://www.orlion.ml/57 2.AOP的产生 对于如下方法:     pub ...

  7. 使用AOP+Annotation实现操作日志记录

    先创建注解 OperInfo @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @ ...

  8. Spring-AOP面向切面编程

    AOP是面向切面编程,区别于oop,面向对象,一个是横向的,一个是纵向. 主要解决代码分散和混乱的问题. 1.概念: 切面:实现AOP共有的类 通知:切面类中实现切面功能的方法 连接点:程序被通知的特 ...

  9. Spring 000 框架简介 (转载)

    转载自:https://my.oschina.net/myriads/blog/37922 1.使用框架的意义与Spring的主要内容 随着软件结构的日益庞大,软件模块化趋势出现,软件开发也需要多人合 ...

随机推荐

  1. AtCoder Grand Contest 014 题解

    A - Cookie Exchanges 模拟 Problem Statement Takahashi, Aoki and Snuke love cookies. They have A, B and ...

  2. js 取get过来的数据

    function getQuery(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&am ...

  3. 使用jpa报No query defined for that name错误

    今天使用jpa创建本地查询时出现Java.lang.IllegalArgumentException: No query defined for that name..... 一个很sb的问题,调用e ...

  4. OS X 10.9 Mavericks下显示和隐藏文件(区别10.8.*)

    我们常常在Windows系统下通过界面设置显示和隐藏文件,在Mac OS X通常采用defaults write命令来解决这个问题. 之前的OS X 10.8.*系统可以使用如下两条命令来开始或者关闭 ...

  5. R语言中的字符处理

    R语言中的字符处理 (2011-07-10 22:29:48) 转载▼ 标签: r语言 字符处理 字符串 连接 分割 分类: R R的字符串处理能力还是很强大的,具体有base包的几个函数和strin ...

  6. 关于android上dpi/screen-size的厘清解释

    android定义了四种screen-size: small normal large xlarge 同时定义了六种dpi级别: ldpi (low) ~120dpimdpi (medium) ~16 ...

  7. p2055&bzoj1433 假期的宿舍

    传送门(洛谷) 传送门(bzoj) 题目 学校放假了······有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如A 和B都是学校的学生,A要回家,而C来看B,C与A不认识. ...

  8. vue.js2.0实战(1):搭建开发环境及构建项目

    Vue.js学习系列: vue.js2.0实战(1):搭建开发环境及构建项目 https://my.oschina.net/brillantzhao/blog/1541638 vue.js2.0实战( ...

  9. 子元素应该margin-top影响父元素的解决办法

    在子元素设置margin-top,有时会带着父元素一起移动. 原因: Outer Div [margin: 0 auto] Inner Div [margin-top: 10px] 根据CSS2.1盒 ...

  10. HTML5与CSS3实例教程(第2版) 附源码 中文pdf扫描版

    HTML5和CSS3技术是目前整个网页的基础.<HTML5与CSS3实例教程(第2版)>共分3部分,集中讨论了HTML5和CSS3规范及其技术的使用方法.这一版全面讲解了最新的HTML5和 ...