基础知识

在 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. 倍增LCA模板2董博文版 伪代码

    Dfs(int rt){ f[][rt]; ;k<=;k++) f[k][rt]=f[k-][f[k-][rt]]; } int LCA(int x,int y){ if(Dp[x]<Dp ...

  2. C语言编译成dll

    首先c语言在开始要加上 #ifdef __cplusplus extern "C" { #endif …被导出的方法名称 #ifdef __cplusplus } #endif 不 ...

  3. redis 数据类型之列表

    1.lpush lpush(name,values) # 在name对应的list中添加元素,每个新的元素都添加到列表的最左边 # 如: # conn.lpush('oo', 11,22,33) # ...

  4. spring boot配置spring-data-jpa的时候报错CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError

    org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager f ...

  5. selenium之窗口滚动

    在这里和大家分享一下,selenium里面常用于处理窗口滚动的方法. location_once_scrolled_into_view 一般用于定位窗口底部元素.将窗口拉到最底部. window.sc ...

  6. SpringBoot使用thymeleaf模板时报错:Template might not exist or might not be accessible by any of the configured Template Resolvers

    错误如下:Template might not exist or might not be accessible by any of the configured Template Resolvers ...

  7. 51Nod 1238 最小公倍数之和V3

    题目传送门 分析: 现在我们需要求: \(~~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j)\) \(=\sum_{i=1}^{n}\sum_{j=1}^{n}\frac ...

  8. 团队第一次作业(软工C#造梦厂)

    一.团队简介 a.团队名称:软工C#造梦厂 b.队员列表 姓名 学号 张旭(组长) 201731024123 周成杰 201731024136 邹扬锋 201731024134 赵俊安 2017310 ...

  9. Python3基础之数据类型(字典)

    Python3数据类型之 字典 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({} ...

  10. 架构模式中的Active Record和Data Mapper

    架构模式中的Active Record和Data Mapper 概念 在简单应用中,领域模型是一种和数据库结构一致的简单结构,对应每个数据库表都有一个领域类,在这种情况下,有必要让每个对象负责数据库的 ...