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的主要内容 随着软件结构的日益庞大,软件模块化趋势出现,软件开发也需要多人合 ...
随机推荐
- Bootstrap CSS教程
Bootstrap 教程 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加 ...
- android开发 如何通过web服务器访问MYSQL数据库并且使其数据同步到android SQLite数据库?
通过web服务器访问MYSQL数据库有以下几个过程: 1.在MySql下创建自己的数据库和自己的表单 2.连接数据库. 3.访问数据库 1.创建web工程 (服务器端) 在Myeclipse下新建一个 ...
- spring IOC 注解@Autowired
自动装配:按照类型来找 会在xml中找类型一样的, 比如 setMessage(SetName setName)上面有@Autowired,会到xml中找<bean id="setna ...
- zabbix的配置
一.网络自动发现: 1.zabbix的网络自动发现是一个非常强大的功能,该功能可以完成以下工作. a.快速发现并添加主机. b.简单的管理. c.随着环境的改变而快速搭建监控系统. 2.网络发现基于以 ...
- 写一个c程序辨别系统是16位or32位
方法: 32位处理器就是一次只能处理32位,也就是4个字节的数据,虚拟地址空间的最大大小是4G,而64位处理一次就能处理64位,即8个字节的数据,最大虚拟地址空间的最大大小是16T.最明显的是指针大小 ...
- [codeforces821E]Okabe and El Psy Kongroo
题意:(0,0)走到(k,0),每一部分有一条线段作为上界,求方案数. 解题关键:dp+矩阵快速幂,盗个图,注意ll 关于那条语句为什么不加也可以,因为我的矩阵C,就是因为多传了了len的原因,其他位 ...
- Jquery常用标签
$(this).hide(1000)//隐藏该元素 $(this).show(1000)//显示该元素 $(this).fadeIn(1000)//淡入已隐藏的元素 $(this).fadeOut(1 ...
- 利用MVC的Area作为二级域名
此处使用的域名是我改了系统的hosts文件达到的 测试成功! 全局的注册方式 在Area的注册文件里进行配置 一个Area和一个外部的Controller 废话不多说,提供DEMO 下载地址
- .Net Core中依赖注入服务使用总结
一.依赖注入 引入依赖注入的目的是为了解耦和.说白了就是面向接口编程,通过调用接口的方法,而不直接实例化对象去调用.这样做的好处就是如果添加了另一个种实现类,不需要修改之前代码,只需要修改注入的地方将 ...
- scrapy-redis源码解读之发送POST请求
1 引言 这段时间在研究美团爬虫,用的是scrapy-redis分布式爬虫框架,奈何scrapy-redis与scrapy框架不同,默认只发送GET请求,换句话说,不能直接发送POST请求,而美团的数 ...