http://zywang.iteye.com/blog/974226

http://www.cnblogs.com/garinzhang/p/java_spring_aop_aspect.html

第一种配置方法:使用@AspectJ标签

  1. 在配置文件中添加<aop:aspectj-autoproxy/>注解
  2. 创建一个Java文件,使用@Aspect注解修饰该类
  3. 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式

说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰

示例:

 1 import org.aspectj.lang.ProceedingJoinPoint;
2 import org.aspectj.lang.annotation.After;
3 import org.aspectj.lang.annotation.AfterThrowing;
4 import org.aspectj.lang.annotation.Around;
5 import org.aspectj.lang.annotation.Aspect;
6 import org.aspectj.lang.annotation.Before;
7 import org.springframework.stereotype.Component;
8
9 /**
10 * 基于注解的AOP日志示例
11 * @author ZYWANG 2011-3-24
12 */
13 @Component
14 @Aspect
15 public class AopLog {
16
17 //方法执行前调用
18 @Before("execution (* com.zywang.services.impl.*.*(..))")
19 public void before() {
20 System.out.println("before");
21 }
22
23 //方法执行后调用
24 @After("execution (* com.zywang.services.impl.*.*(..))")
25 public void after() {
26 System.out.println("after");
27 }
28
29 //方法执行的前后调用
30 @Around("execution (* com.zywang.services.impl.*.*(..))")
31 public Object around(ProceedingJoinPoint point) throws Throwable{
32 System.out.println("begin around");
33 Object object = point.proceed();
34 System.out.println("end around");
35 return object;
36 }
37
38 //方法运行出现异常时调用
39 @AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex")
40 public void afterThrowing(Exception ex){
41 System.out.println("afterThrowing");
42 System.out.println(ex);
43 }
44 }

上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:

 1 import org.aspectj.lang.ProceedingJoinPoint;
2 import org.aspectj.lang.annotation.Around;
3 import org.aspectj.lang.annotation.Aspect;
4 import org.aspectj.lang.annotation.Before;
5 import org.aspectj.lang.annotation.Pointcut;
6 import org.springframework.stereotype.Component;
7
8 /**
9 * 基于注解的AOP日志示例
10 * @author ZYWANG 2011-3-24
11 */
12 @Component
13 @Aspect
14 public class AopLog {
15
16 @Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))")
17 public void pointcut(){}
18
19 //方法执行前调用
20 @Before("pointcut()")
21 public void before() {
22 System.out.println("before");
23 }
24
25 //方法执行的前后调用
26 @Around("pointcut()")
27 public Object around(ProceedingJoinPoint point) throws Throwable{
28 System.out.println("begin around");
29 Object object = point.proceed();
30 System.out.println("end around");
31 return object;
32 }
33 }

第二种配置方法:基于配置文件的配置

  1. 创建一个Java文件,并指定一个用于执行拦截的方法,该方法可以有0个或多个参数
  2. 在Spring配置文件中注册该Java类为一个Bean
  3. 使用<aop:config/>、<aop:aspect/>等标签进行配置

示例:

Java文件

 1 import org.aspectj.lang.ProceedingJoinPoint;
2
3 /**
4 * 基于配置文件的AOP日志示例
5 * @author ZYWANG 2011-3-24
6 */
7 public class AopLog {
8
9 //方法执行的前后调用
10 public Object runOnAround(ProceedingJoinPoint point) throws Throwable{
11 System.out.println("begin around");
12 Object object = point.proceed();
13 System.out.println("end around");
14 return object;
15 }
16
17 }

使用第二种方式的AOP配置,在Eclipse(有SpringIDE插件)中被拦截到的方法中有标识显示

以上配置基于Spring 3.0.5 进行设置,参考其《Reference Documentation》

Spring3.0中的AOP配置方法的更多相关文章

  1. WCF学习之旅—WCF4.0中的简化配置功能(十五)

    六 WCF4.0中的简化配置功能 WCF4.0为了简化服务配置,提供了默认的终结点.绑定和服务行为.也就是说,在开发WCF服务程序的时候,即使我们不提供显示的 服务终结点,WCF框架也能为我们的服务提 ...

  2. AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法将部分汉字编码为乱码的解决方案

    AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法将部分汉字编码为乱码的解决方案 以下代码为asp.net环境下,c#语言编写的解决方案.数据用Dictiona ...

  3. CentOS-7.0.中安装与配置Tomcat-7的方法

    安装说明 安装环境:CentOS-7.0.1406安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz 下载地址:http://tomcat.apache.org/down ...

  4. spring3.0.5的aop使用

    spring3.0.5开始支持jpa2.0了,但是最近笔者在使用他的的时候发现了3.0.5的包与2.5.5相比,有所精简.其他外部的包,我们需要自己下载. AOP必须的spring包 org.spri ...

  5. django 2.0 中URL的include方法使用分析

    一.问题出现: 在使用Django2.0,配置全局URL时,希望指向某个APP的URL,配置如下: from django.contrib import admin from django.conf. ...

  6. AFNetworking 3.0中调用[AFHTTPSessionManager manager]方法导致内存泄漏的解决办法

    在使用AFNetworking3.0框架,使用Instruments检查Leaks时,检测到1000多个内存泄漏的地方,定位到 [AFHTTPSessionManager manager] 语句中,几 ...

  7. Vue2.0中的路由配置

    Vue2.0较Vue1.0,路由有了较大改变.看一下Vue2.0中的路由如何配置: 步骤一: 在main.js文件中引入相关模块以及组件及实例化vue对象配置选项路由及渲染App组件 默认设置如下: ...

  8. vue3.0中的双向数据绑定方法

    熟悉vue的人都知道在vue2.x之前都是使用object.defineProperty来实现双向数据绑定的 而在vue3.0中这个方法被取代了 1. 为什么要替换Object.definePrope ...

  9. VS2015ASP.NET MVC5项目中Spring.NET配置方法(超详细)

    首先,在ASP.NET MVC5项目右键,如下图所示,选择“管理Nuget程序包...” 然后,在弹出的页面的搜索框中输入“spring.web”,在返回结果中选择Spring.Web和Spring. ...

  10. Spring3数据源的6种配置方法

    在Spring3中,配置DataSource的方法有五种. 第一种:beans.xml <bean id="dataSource" class="org.apach ...

随机推荐

  1. Redis的设计与实现-总结

    个人真的很喜欢这本书, 从对C语言一窍不通, 到发现C语言竟然如此简洁, 以至于我喜欢上了C! 对此前面的底层数据结构也读了几次, 大致整理了书里的内容, 后面的就粗略看了一下, 不再细细整理了. R ...

  2. Blazor中用浏览器打开一个链接的最好方法

    适用于Blazor Wasm和Blazor SSR 调用下面的js方法 说一下为什么不用window.open,有可能被拦截是小问题,大问题是打开新页面未加载完时,回到原页面,大概率卡死,无法点击任何 ...

  3. 一个从文件中过滤指定字符串的python3脚本

    from tabulate import tabulate plugin = [ ... ] plugin_v1 = [ ... ] filepath = "E:\\PycharmProje ...

  4. 2022-1-11 面板控件学习1 Canvas、WrapPanel、StackPanel、DockPanel、Grid

    Canvas WrapPanel 让控件横向排列 StackPanel 控件竖向排列 DockPanel 自由布局,LastChildFill防止最后一个控件自动填充满 Grid 使用*和2*可以分配 ...

  5. Go 语言 select 都能做什么?

    原文链接: Go 语言 select 都能做什么? 在 Go 语言中,select 是一个关键字,用于监听和 channel 有关的 IO 操作. 通过 select 语句,我们可以同时监听多个 ch ...

  6. Vue报错:Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location

    错误原因,我猜测多半是版本问题 在router/index.js中添加如下代码 const originalPush = VueRouter.prototype.push VueRouter.prot ...

  7. 简述分布式链路追踪工具——Jaeger

    1.简介 1.1 Jaeger是什么 Jaeger  是受到 ​ ​Dapper​​​ 和 ​ ​OpenZipkin​​​ 启发的由 ​ ​Uber Technologies​​ 作为开源发布的分布 ...

  8. chatglm2-6b模型在9n-triton中部署并集成至langchain实践

    一.前言 近期, ChatGLM-6B 的第二代版本ChatGLM2-6B已经正式发布,引入了如下新特性: ①. 基座模型升级,性能更强大,在中文C-Eval榜单中,以51.7分位列第6: ②. 支持 ...

  9. 如何在linux上安装neovim0.9(以debian和ubuntu为例) – 东凭渭水流

    发布于 1 分钟前  3 次阅读 由于apt中只有neovim-0.72的安装包.想使用新版需要自己安装,以下是安装过程 1.首先需要卸载旧版neovim sudo remove neovim 2.从 ...

  10. 《CTFshow-Web入门》03. Web 21~30

    @ 目录 web21 题解 原理 web22 题解 原理 web23 题解 原理 web24 题解 原理 web25 题解 原理 web26 题解 web27 题解 web28 题解 web29 题解 ...