1、pom.xml追加

spring-aspects

aspectjrt

为控制器以外的类织入切面

2、新建spring-aop.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="io.deolin.demoapp.service" /> <aop:aspectj-autoproxy /> <bean class="io.deolin.demoapp.aspect.ServiceAspect" /> </beans>

扫描控制层以外的包

3、新建切面

@Aspect
public class ServiceAspect extends AspectCommon { Logger log = LogManager.getLogger(ServiceAspect.class); @Pointcut("execution(* io.deolin.demoapp.service.*.*(..))")
public void performance() {} @Before("performance()")
public void before(JoinPoint joinPoint) {
log.info(getFinger(joinPoint) + " 业务开始");
} @AfterReturning("performance()")
public void afterReturning(JoinPoint joinPoint) {
log.info(getFinger(joinPoint) + " 业务结束");
} @AfterThrowing(value = "performance()", throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Throwable e) throws Throwable {
log.error(getFinger(joinPoint) + " 业务异常 " + e.getClass().getSimpleName());
} }
abstract public class AspectCommon {

    protected String getFinger(JoinPoint joinPoint) {
String className = joinPoint.getTarget().getClass().getSimpleName();
String mehtodName = joinPoint.getSignature().getName();
return className + "#" + mehtodName;
} }

为控制器织入切面

4、dispatcherservlet-servlet.xml追加

    <!-- 控制层AOP -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean class="io.deolin.demoapp.aspect.ControllerAspect" />

5、新建切面

@Aspect
public class ControllerAspect extends AspectCommon { Logger log = LogManager.getLogger(ControllerAspect.class); @Pointcut("execution(* io.deolin.demoapp.controller.*.*(..))")
public void performance() {} @Before("performance()")
public void before(JoinPoint joinPoint) {
log.info(getFinger(joinPoint) + " 请求开始");
} @AfterReturning("performance()")
public void afterReturning(JoinPoint joinPoint) {
log.info(getFinger(joinPoint) + " 请求结束");
} }

最新文章

  1. HashBytes(Transact-SQL)
  2. IOS block 循环引用的解决
  3. Eclipse代码追踪功能说明
  4. 【leetcode】Reverse Linked List II (middle)
  5. hybrid app 简介
  6. Java集合 Json集合之间的转换
  7. [001]const和指针
  8. shell 基础 $(cd `dirname $0`;pwd)
  9. 为什么这么多Python框架
  10. VirtulBox虚拟机搭建Linux Centos系统
  11. hdu_1251统计难题(字典树Trie)
  12. RePr: Improved Training of Convolutional Filters
  13. Mysql数据库远程链接、权限修改、导入导出等基本操作
  14. 【Android】Android EditText 去除边框
  15. DataGridView获取或者设置当前单元格的内容
  16. Centos7.3+uwsgi+Nginx部署Django程序
  17. CMake命令
  18. UWP Button添加圆角阴影(二)
  19. GitLab概念——Group、Project、Member
  20. SPREAD for Windows Forms 下箭头追加行

热门文章

  1. idea jetty:run 启动
  2. Interlocked
  3. 1 Refused to display ‘url’ in a frame because it set &#39;X-Frame-Options&#39; to &#39;sameorigin&#39; 怎么解决?
  4. [学习笔记]pb_ds库
  5. Python练习_Python初识_day2
  6. 微信小程序 上传图片并等比列压缩到指定大小
  7. 移动端适配flexible.js
  8. Go微服务 grpc/protobuf
  9. git remote 使用总结
  10. Flutter学习之Dart语言基础(构造函数)