SpringAOP配置与使用(示例)
2024-08-27 21:30:30
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) + " 请求结束");
} }
最新文章
- HashBytes(Transact-SQL)
- IOS block 循环引用的解决
- Eclipse代码追踪功能说明
- 【leetcode】Reverse Linked List II (middle)
- hybrid app 简介
- Java集合 Json集合之间的转换
- [001]const和指针
- shell 基础 $(cd `dirname $0`;pwd)
- 为什么这么多Python框架
- VirtulBox虚拟机搭建Linux Centos系统
- hdu_1251统计难题(字典树Trie)
- RePr: Improved Training of Convolutional Filters
- Mysql数据库远程链接、权限修改、导入导出等基本操作
- 【Android】Android EditText 去除边框
- DataGridView获取或者设置当前单元格的内容
- Centos7.3+uwsgi+Nginx部署Django程序
- CMake命令
- UWP Button添加圆角阴影(二)
- GitLab概念——Group、Project、Member
- SPREAD for Windows Forms 下箭头追加行
热门文章
- idea jetty:run 启动
- Interlocked
- 1 Refused to display ‘url’ in a frame because it set &#39;X-Frame-Options&#39; to &#39;sameorigin&#39; 怎么解决?
- [学习笔记]pb_ds库
- Python练习_Python初识_day2
- 微信小程序 上传图片并等比列压缩到指定大小
- 移动端适配flexible.js
- Go微服务 grpc/protobuf
- git remote 使用总结
- Flutter学习之Dart语言基础(构造函数)