spring aop切面应用,记录日志、请求处理耗时
来源:互联网 发布:淘宝靠谱俄罗斯代购 编辑:程序博客网 时间:2024/05/18 05:01
依赖jar包
aopalliance.jar、aspectjrt.jar、aspectjweaver-1.6.jar
spring配置文件(着重aop部分配置):
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><context:property-placeholder location="classpath*:application.properties" /><context:component-scan base-package="com.csp.iobs" /><aop:aspectj-autoproxy/>
切面代码(环绕通知):
package com.test.aspect;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;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.springframework.stereotype.Component;@Aspect@Componentpublic class ControllerAspect {private static final Log LOG = LogFactory.getLog(ControllerAspect.class);@Pointcut(value = "execution(public * com.test.controller.*.*(..))")public void recordLog() {}/** * 所拦截方法执行之前执行 */@Before("recordLog()")public void before(){System.out.println("before---------------------------------------");}/** * 同时在所拦截方法的前后执行 *在ProceedingJoinPoint.proceed()前后加逻辑 */@Around("execution(public * com.test.controller.*.*(..))")public void around(ProceedingJoinPoint joinPoint) throws Throwable {long startTime = System.currentTimeMillis();joinPoint.proceed();//个人理解是去执行相应的Controller方法long costTime = System.currentTimeMillis() - startTime;String methodName = joinPoint.getSignature().getName();//记录执行请求耗时LOG.info(methodName + " finished ! Cost : " + costTime + " ms.");}/** * 所拦截方法执行之后执行 */@After("recordLog()")public void after(){System.out.println("after---------------------------------------");}}
推荐文章地址:http://outofmemory.cn/code-snippet/3025/spring-AOP-Around-Before-After-differentiate
1 0
- spring aop切面应用,记录日志、请求处理耗时
- Spring 切面 记录请求日志
- spring aop 切面记录log4j日志
- Spring aop切面做日志处理操作
- aop切面记录日志
- Spring-Aop处理记录日志
- spring-Aop应用日志处理
- Spring—AOP实践,利用切面记录用户操作日志
- AOP切面进行日志记录
- spring aop 切面记录log4j日志 之around记录异常信息
- spring aop 日志记录
- Spring AOP记录日志!
- Spring AOP记录日志! .
- spring aop记录日志
- Spring AOP 记录日志
- Spring AOP日志记录
- Spring AOP 记录日志
- spring AOP--记录日志
- SSH安全加固
- Android studio 导入第三方类库工程 转
- Java——父子实例的内存情况解析
- CAS(7)-建立使用Cas进行单点登录的应用
- node.js读取本地的文件
- spring aop切面应用,记录日志、请求处理耗时
- theme为dialog的Activity如何充满全屏
- 重置ip
- Spring常用注解 使用注解来构造IoC容器
- request实现页面包含
- 钉钉微应用获取身份
- 博客资源
- 怎样使用Navicat for Mysql连接Ubuntu虚拟机上的mysql服务器
- JavaScript创造对象