SpringAOP配置与使用(示例)
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) + " 请求结束");
} }
SpringAOP配置与使用(示例)的更多相关文章
- springcloud(七):配置中心svn示例和refresh
上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...
- 利用JavaScriptSOAPClient直接调用webService --完整的前后台配置与调用示例
JavaScriptSoapClient下载地址:https://archive.codeplex.com/?p=javascriptsoapclient JavaScriptSoapClient的D ...
- 分布式大数据多维分析(OLAP)引擎Apache Kylin安装配置及使用示例【转】
Kylin 麒麟官网:http://kylin.apache.org/cn/download/ 关键字:olap.Kylin Apache Kylin是一个开源的分布式分析引擎,提供Hadoop之上的 ...
- linux之vim配置及使用示例
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7056193.html linux之vim配置及使用示例 vi的三种模式: 一 ...
- springAOP配置原理
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...
- springcloud(六):配置中心git示例
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...
- 华为AR配置内部服务器示例(只有1个公网IP)
AR配置公网和私网用户都可以通过公网地址访问内部服务器示例(只有1个公网IP) 适用于:V200R003C01及以后的系统软件版本. 组网需求: 由于只有1个公网IP(100.100.1.2),想实现 ...
- Echarts图表常用功能配置,Demo示例
先看下效果图: 就如上图所示,都是些常用的基本配置. Legend分页,X轴设置,Y轴设置,底部缩放条设置, 数值显示样式设置,工具箱设置,自定义工具按钮, 绑定点击事件等等.这些配置代码中都做了简单 ...
- Web Api跨域访问配置及调用示例
1.Web Api跨域访问配置. 在Web.config中的system.webServer内添加以下代码: <httpProtocol> <customHeaders> &l ...
随机推荐
- raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
转自:http://www.cnblogs.com/xiaobinglife/articles/10716605.html 一.Django数据同步过程中遇到的问题: 1.raise Improper ...
- (五)springmvc之获取表单提交的数据
8.1:使用Request <form method="post" id="form1" action="<%=request.getCo ...
- Mybatis分页的方式以及实现
方式 分页的分类: 1.物理分页:只从数据库中查询当前页的数据 优点:不占用很多内存 缺点:效率比价低(相比于逻辑分页) 2.逻辑分页:从数据库将所有记录查询出来,存储到内存中,展示当前页,然后数 ...
- 利用Supervisor 管理自己部署的应用程序
首先,在centos7下安装supervisor yum install python-setuptools easy_install supervisor 然后新建配置文件 #新建superviso ...
- 动态规划-最大算式 蓝桥杯ALGO-116
问题描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就是N-1个了,所以恰好每两个相邻数字之间都有一个符号 ...
- VBA连接操作符
VBA支持以下连接运算符. 假设变量A=5,变量B=10,则 - 运算符 描述 示例 + 将两个值添加为变量,其值是数字 A + B = 15 & 连接两个值 A & B = 510 ...
- OpenCV手工实现灰度及RGB直方图
手工实现灰度及RGB直方图 !库 1. 灰度图像直方图 算法 1. 图片灰度化: 2. 遍历Mat,统计各灰度级的像素个数: 3. 根据opencv画点线函数,绘制坐标轴及像素分布图 源码(编译环境: ...
- 开源证书检查工具:fossy(fossology)
工具下载: https://github.com/fossology/fossology 其他说明: http://archive15.fossology.org/projects/fossology ...
- pymysql 1064, 'You have an error in your SQL syntax; check the manual that corresponds to
在python 连接mysql时,最近一直出现了 1064, 'You have an error in your SQL syntax; check the manual that correspo ...
- GNS3
什么是GNS? GNS Graphical Network Simulator Simulator or Emulator? 尽管GNS全拼包含simulator,但实际上是emulator.我们说其 ...