基础知识

在 Spring AOP 中,有 3 个常用的概念,Advices 、 Pointcut 、 Advisor ,解释如下:

  • Advices :表示一个 method 执行前或执行后的动作。
  • Pointcut :表示根据 method 的名字或者正则表达式去拦截一个 method 。
  • Advisor : Advice 和 Pointcut 组成的独立的单元,并且能够传给 proxy factory 对象。

用xml配置Pointcut

<bean id="Pointcut的Id" class="org.springframework.aop.support.NameMatchMethodPoint">
<property name="mappedName" vlaue="方法名"/>
</bean>

用xml配置Advisor,将pointcut与advice关联起来

<bean id="Advisor的Id" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="要关联的Pointcut"/>
<property name="advice" ref="要关联的Advice"/>
</bean>

用xml配置代理类

<bean id="生成的代理的Id" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="要代理的类(即需要劫持的类)"/>
<property>
<list>
<value>Advisor的Id</value>
</list>
</property>
</bean>

不单独配置Pointcut,在配置Advisors时class选择NameMatchMethodPointcutAdvisor

<bean id="Advisor的Id" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="方法名"/>
<property name="advice" ref="要关联的Advice" />
</bean>

创建使用正则表达式匹配要拦截的method的切点

在class中选择org.springframework.aop.support.RegexpMethodPointcutAdvisor

<bean id="Advisor名" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>.*URL.*</value>
</list>
</property>
<property name="advice" ref=""/>
</bean>

Spring学习笔记:使用Pointcut 和Advisor实现AOP的更多相关文章

  1. Java框架spring 学习笔记(十二):aop实例操作

    使用aop需要在网上下载两个jar包: aopalliance.jar aspectjweaver.jar 为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将 ...

  2. spring学习笔记(三)我对AOP理解

    首先我们要知道AOP是什么?AOP全称Aspect OrientedProgramming,即面向切面编程.在这里我不想去说什么是切面,什么是切点,什么是通知等等,相关博客很多,如果大家想知道可以自己 ...

  3. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  4. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  5. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  6. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  7. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  8. 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面

    作者:ssslinppp       异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...

  9. 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

    作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...

随机推荐

  1. C# 删除指定文件

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;us ...

  2. 原生javascript 基础动画函数封装(一)

    <!DOCTYPE html> <html> <head> <title></title> <style type="tex ...

  3. cannot mount volume over existing file, file exists /var/lib/docker/overlay2/.../merged/usr/share/zoneinfo/UTC 解决

    问题产生原因: linux系统docker-compose.yml文件 放到 mac本启动发现启动报错 cannot mount volume over existing file, file exi ...

  4. mongo repository

    using MongoDB.Driver; namespace Dben.Invoice.Repository { /// <summary> /// 仓储基类 /// </summ ...

  5. JVM系列七(JIT 即时编译器).

    一.概述 即时编译器(Just In Time Compiler),也称为 JIT 编译器,它的主要工作是把热点代码编译成与本地平台相关的机器码,并进行各种层次的优化,从而提高代码执行的效率. 那么什 ...

  6. <a>标签的href和onclick属性【转】

    1链接的onclick 事件被先执行,其次是href属性下的动作(页面跳转,或 javascript 伪链接): 2假设链接中同时存在href 与onclick,如果想让href 属性下的动作不执行, ...

  7. 小白学Java:奇怪的RandomAccess

    目录 小白学Java:奇怪的RandomAccess RandomAccess是个啥 forLoop与Iterator的区别 判断是否为RandomAccess 小白学Java:奇怪的RandomAc ...

  8. dp——洛谷 P1541 乌龟棋 —— by hyl天梦

    题目:(转自 https://www.luogu.com.cn/problem/P1541) 题目描述 乌龟棋的棋盘是一行NN个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第NN格是 ...

  9. [bzoj3529] [洛谷P3312] [Sdoi2014] 数表

    Description 有一张n×m的数表,其第i行第j列(1 < =i < =n,1 < =j < =m)的数值为 能同时整除i和j的所有自然数之和.给定a,计算数表中不大于 ...

  10. xpath写法汇总(适用于appium、robotframework)

    1.移动端应用,content-des包含“订单号” xpath=//android.view.View[contains(@content-desc,"订单号:")] 2.移动端 ...