用基于 XML 的配置声明切面

除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的.

正常情况下, 基于注解的声明要优先于基于 XML 的声明. 通过 AspectJ 注解, 切面可以与 AspectJ 兼容, 而基于 XML 的配置则是 Spring 专有的. 由于 AspectJ 得到越来越多的 AOP 框架支持, 所以以注解风格编写的切面将会有更多重用的机会.

基于 XML ---- 声明切面

当使用 XML 声明切面时, 需要在 <beans> 根元素中导入 aop Schema

在 Bean 配置文件中, 所有的 Spring AOP 配置都必须定义在 <aop:config> 元素内部. 对于每个切面而言, 都要创建一个 <aop:aspect> 元素来为具体的切面实现引用后端 Bean 实例.

切面 Bean 必须有一个标示符, 供 <aop:aspect> 元素引用

声明切面的实例代码

基于 XML ---- 声明切入点

切入点使用 <aop:pointcut> 元素声明

切入点必须定义在 <aop:aspect> 元素下, 或者直接定义在 <aop:config> 元素下.

– 定义在 <aop:aspect> 元素下: 只对当前切面有效

– 定义在 <aop:config> 元素下: 对所有切面都有效

基于 XML 的 AOP 配置不允许在切入点表达式中用名称引用其他切入点.

声明切入点的示例代码

<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置切面 --><bean id="loggingAspect" class="com.learn.spring.aspect_xml.LoggingAspect"></bean><bean id="validatorAspect" class="com.learn.spring.aspect_xml.ValidatorAspect"></bean><!-- 配置目标bean --><bean id="arithmeticCalculatorImpl" class="com.learn.spring.aspect_xml.ArithmeticCalculatorImpl"></bean><!-- 配置aop --><aop:config><!-- 配置切入点表达式 --><aop:pointcut expression="execution(* com.learn.spring.aspect_xml.*.*(..))"id="myPointCut"/><!-- 配置切面及通知 --><aop:aspect ref="loggingAspect"  order="1"><aop:before method="beforeMethod" pointcut-ref="myPointCut"/><aop:after method="afterMethod" pointcut-ref="myPointCut"/><aop:after-returning method="afterReturningMethod" pointcut-ref="myPointCut" returning="result"/><aop:after-throwing method="afterThrowingMethod" pointcut-ref="myPointCut" throwing="ex"/></aop:aspect><!-- <aop:aspect order="2"></aop:aspect> --></aop:config></beans>

基于 XML ---- 声明通知

在 aop Schema 中, 每种通知类型都对应一个特定的 XML 元素.

通知元素需要使用 <pointcut-ref> 来引用切入点, 或用 <pointcut> 直接嵌入切入点表达式.  method 属性指定切面类中通知方法的名称.

基于xml的方式配置AOP相关推荐

  1. xml的方式配置AOP:Aspect Oriented Programming

    在某些类中, 什么时机, 做什么事情  切入点(point-cut): 在某些类中(Class<?>[] itfc = new Class<?>[] { IStudentSer ...

  2. ❤️基于Java的方式配置Spring

    ❤️基于Java的方式配置Spring 完全不使用Spring的xml配置,全权交给java来做! JavaConfig 是Spring的一个子项目.在Spring之后,成为了一个核心功能! ​ 测试 ...

  3. html下拉菜单读取xml,基于XML的可配置Html下拉框的设计与实现

    (江西制造职业技术学院信息工程系,江西 南昌 330000) 摘 要:专门针对Web下拉框控件影响网页性能.不利于美工和维护等问题,通过比较Web下拉框与Html下拉框各自的利弊,提出一种基于XML实 ...

  4. 使用XML文件方式配置log4cxx(log4cxx xml例子)

    废话不多说,直接给出log.xml文件的全部内容. xml例子)" name=image_operate_59151337264079188 alt="使用XML文件方式配置log ...

  5. 基于XML及注解配置方式实现AOP及aspectJ表达式

    aspectJ表达式结构 切入点表达式 execution (* com.sample.service.impl...(..)) 1.execution(): 表达式主体. 2.第一个号:表示返回类型 ...

  6. java学习day40(Spring)spring中的aop和基于XML以及注解的AOP配置

    第1章 AOP 的相关概念[理解] 1.1AOP 概述 1.1.1 什么是 AOP AOP :全称是 Aspect Oriented Programming 即:面向切面编程. 简单的说它就是把我们程 ...

  7. spring框架的概述以及spring中基于XML的IOC配置——概念

    1.spring的概述     spring是什么     spring的两大核心     spring的发展历程和优势     spring体系结构 2.程序的耦合及解耦     曾经案例中问题   ...

  8. spring 基于xml方式配置aop

    目录 什么是aop 模拟aop 配置 什么是aop 什么是aop 作用 在程序运行期间,在不修改源码的情况下对方法进行功能增强 优势 减少重复代码 提高开发效率 并且便于开发 2.aop关键概念 模拟 ...

  9. 基于注解的方式配置bean

    在 classpath 中扫描组件 组件扫描(component scanning):  Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. 特定组件包括: @C ...

最新文章

  1. vim中设置python代码缩进为4个空格
  2. Jury Compromise POJ - 1015
  3. 从外部CorDapp扩展和覆盖流
  4. java io编程_Java_IO编程
  5. python之路_自定义属性、json及其他js补充
  6. opencv python教程-OpenCV4 Python 最新中文版官方教程来了(附下载)
  7. 鸟哥linux php,鸟哥的 Linux 私房菜 -- 启动关机、在线求助与命令下达方式
  8. 1.4 高并发之线程和进程
  9. 9.Shell 编程从入门到精通 --- 进程
  10. Arduino(c) 16进制转字符串
  11. 多因素deseq2 formula 怎么理解 如何设置design 哈佛大学——差异表达分析(七)设计公式(Design formulas)多因素差异分析 多个影响因子会影响差异分析结果 多因子
  12. freeCodeCamp Falsy Bouncer
  13. HTTP请求头及其作用
  14. IIC扩展IO NCA9555代码
  15. 晶振电路并联一个电阻作用
  16. win7系统中的消息队列服务器,高手分析win7系统安装消息队列的详细
  17. RHCSA之Linux系统简介
  18. 什么是微信不死域名 微信不死域名的原理是什么
  19. Apple developer新的的注册方式
  20. 网御数据库审计系统配置Radius启用双因素/双因子(2FA/MFA)认证

热门文章

  1. Git 添加到Git 仓库
  2. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
  3. Linux文件权限查看及修改命令chmod,chown
  4. 卡特兰数 BZOJ3907 网格 NOIP2003 栈
  5. cdoj 秋实大哥搞算数
  6. Android视图组成View
  7. 运营一个网站,新增加一个功能,容易忽视哪些问题就匆匆上线?
  8. 如何用“区块链+稳定币”技术来提升资产证券化市场运作效率
  9. 天地图之定位信息详解
  10. Common sql statement