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 ...
随机推荐
- (三)自定义Realm
一.Realm概念 Realm:域,Shiro从从Realm获取安全数据(如用户.角色.权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用 ...
- C# 关于爬取网站数据遇到csrf-token的分析与解决
需求 某航空公司物流单信息查询,是一个post请求.通过后台模拟POST HTTP请求发现无法获取页面数据,通过查看航空公司网站后,发现网站使用避免CSRF攻击机制,直接发挥40X错误. 关于CSRF ...
- Linux查看进程并重启服务命令
top -u root 查看系统进程service network restartservice iptables restartservice sshd restartservice nginx r ...
- puml 用于代码注释
notebook 笔记本 @startuml rectangle sql_decode.py{ object SQLDataset object Name SQLDataset : meta = &q ...
- # 机器学习算法总结-第九天(XGboost)
- iOS 超级签名详解
一.原理 把安装设备当做开发设备进行分发.说的明白一些,开发者可以在开发者后台添加手机的UDID,然后重新打包一个IPA文件,分发平台,然后被添加的UDID就可以下载. 二.优缺点 优势: 直接分发, ...
- 你不知道的javascript(上卷)读后感(二)
this词法 熟悉ES6语法的开发者,箭头函数在涉及this绑定时的行为和普通函数的行为完全不一致.跟普通this绑定规则不一样,它使用了当前的词法作用域覆盖了this本来的值. 误解 this理解成 ...
- Hadoop集群搭建(cluster setup),ssh免密后一直要求输入密码的原因
前段时间,网上有言SHA-1加密技术,已经被谷歌公司破解,在linux系统中,集群间加密的技术是用DSA秘钥,秘钥本身其实是一种算法,就像前面说的SHA-1也是加密算法的一种. 免密在linux系统中 ...
- selenium网页截图和截图定位(带界面)
from selenium import webdriver import time from PIL import Image driver = webdriver.Chrome() driver. ...
- 【appium】appium中的元素定位和基本操作
# coding=utf-8 from appium import webdriver import time from selenium.webdriver.support.ui import We ...