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 ...
随机推荐
- C# 第一次做项目。一些经验总结。
这是我的第一篇博客,写得不好望大家多多包涵. 初学C#2个多月,拿着老师给的项目,试着做了做,发现自己在编程方面有很多陋习与编程知识方面的不足. 首先是没有遵守某一个设计模式,这导致我想到哪里就做到了 ...
- sqlserver使用EF模型经验
sqlserver使用EF模型经验 EF模型使用本人在之前两三年中从没使用过,所以刚开始使用就会踩上许多的坑.今天我不单单说下自己所踩的一些坑与当前公司中使用EF模型设计的理念,即是为我自己做个笔记, ...
- 把一个质量为M0 的物体加速到 时间慢100倍 需要多大能量
洛伦兹系数 γ = 100: 运动时候的质量 M = γM0 速度 v = c*sqrt(1-1/γ2) E = 1/2 * m0c2 *γ* (1-1/γ2) 如果一个60kg 的人想要加 ...
- POJ1083(Moving Tables)--简单模拟
题目链接:http://poj.org/problem?id=1083 如图所示在一条走廊的两侧各有200个房间,现在给定一些成对的房间相互交换桌子,但是走廊每次只能通过一组搬运, 也就是说如果两个搬 ...
- 关于微信小程序发布新版本后的提示用户更新的方法详解
当小程序发布新的版本后 ,用户如果之前访问过该小程序,通过已打开的小程序进入(未手动删除),则会检测新版本,提醒用户更新新的版本 话不多说,上代码 App({ onLaunch: function ( ...
- clipboard兼容各个浏览器,不需要设置权限
解决方案:用clipboard.js插件 注意要引用clipboard.js文件 案例: 将文本赋值给剪切板 <button class="btn">Copy</ ...
- 2018年长沙理工大学第十三届程序设计竞赛 I 连续区间的最大公约数
连续区间的最大公约数 思路:参照BZOJ 4488: [Jsoi2015]最大公约数脑补出的一个\(map\)套\(vector\)的写法,写起来比线段树短,运行时间比线段树快. 代码: #pragm ...
- hive中使用spark执行引擎的常用参数
set hive.execution.engine=spark;set hive.exec.parallel=true;set hive.exec.parallel.thread.number=8;s ...
- centos6.5 安装emqtt
emqtt有监控界面,集群配置也非常简单. # yum -y update # yum install unzip vim wget xsltproc fop tk unixODBC unixODBC ...
- jQuery获取父窗口的元素
js获取父页面的元素可以用 $(window.parent.document).find("#customer_id").val();这里的customer_id表示父页面某一个元 ...