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. BZOJ4317: Atm的树+2051+2117

    BZOJ4317: Atm的树+2051+2117 https://lydsy.com/JudgeOnline/problem.php?id=4317 分析: 二分答案之后就变成震波那道题了. 冷静一 ...

  2. ACM学习历程—HDU 5072 Coprime(容斥原理)

    Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...

  3. from selenium.webdriver.support.ui import Select

    from selenium.webdriver.support.ui import Select Select(d.find_element_by_id(u'key_开户行')).first_sele ...

  4. netty中的引导Bootstrap服务端

    引导一个应用程序是指对它进行配置,并使它运行起来的过程. 一.Bootstrap 类 引导类的层次结构包括一个抽象的父类和两个具体的引导子类,如图 8-1 所示 服务器致力于使用一个父 Channel ...

  5. ubuntu dd烧录镜像文件

    df命令查看挂在的U盘和硬盘,ssd 右边可以看到路径 sudo dd if=cn_windows_10_multiple_editions_version_1703_updated_july_201 ...

  6. Java类的生命周期(转)

    引言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑,笔者打开百度搜了一下相关的问题,看到网上的资料很少有把这个问题讲明白的,主要是因为目前国内java方面的教材大多只是告 ...

  7. <正则吃饺子> :关于mybatis中使用的问题(一)

    在公司项目开始之前,根据springboot .mybatis.Swagger2 整合了一个demo,在测试时候,遇到的问题,简单记录.之前在使用mybatis时候,没有注意到这一点. 1.错误:Th ...

  8. source in sight 删除工程

    用十六进制编辑器打开  "我的文档/Source Insight/Projects/PROJECTS.DB3" 文件 ,找到你要删除的项目路径及名称字符串,用0替换相关位置的数据.

  9. Java学习路线-知乎

    鼬自来晓 378 人赞同 可以从几方面来看Java:JVM Java JVM:内存结构和相关参数含义 · Issue #24 · pzxwhc/MineKnowContainer · GitHub J ...

  10. VS 关于无法打开项目文件,此安装不支持该项目类型的问题

    用VS打开后有时会出现类似: 无法打开项目文件,此安装不支持该项目类型 的错误,这个错误一般都是由于用低版本VS打开高版本项目文件造成的 其中包括: 1.用VS2003 打开包括VS2005以上版本项 ...