Spring 框架的一个关键组件是面向方面的编程(AOP)框架。面向方面的编程需要把程序逻辑分解成不同的部分称为所谓的关注点。跨一个应用程序的多个点的功能被称为横切关注点,这些横切关注点在概念上独立于应用程序的业务逻辑。有各种各样的常见的很好的方面的例子,如日志记录、审计、声明式事务、安全性和缓存等。

一些AOP术语:

(1) aspect:一个模块具有一组提供横切需求的 APIs。例如,一个日志模块为了记录日志将被 AOP 方面调用。应用程序可以拥有任意数量的方面,这取决于需求。
(2) joint point:相当于ABAP类里可以被增强的method
(3) advice:这是实际行动之前或之后执行的方法。这是在程序执行期间通过 Spring AOP 框架实际被调用的代码。相当于ABAP的pre/post/overwrite exit.
(4) weaving: Weaving 把方面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时,类加载时和运行时完成。

为了使用 aop 命名空间标签,需要导入 spring-aop j架构,如下所述:

Beans.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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><!-- bean definition & AOP specific configuration --></beans>

看个具体的例子:

(1) 新建一个Logging.java:

public class Logging {/** * This is the method which I would like to execute* before a selected method execution.*/public void beforeAdvice(){System.out.println("Going to setup student profile.");}/** * This is the method which I would like to execute* after a selected method execution.*/public void afterAdvice(){System.out.println("Student profile has been setup.");}/** * This is the method which I would like to execute* when any method returns.*/public void afterReturningAdvice(Object retVal){System.out.println("Returning:" + retVal.toString() );}/*** This is the method which I would like to execute* if there is an exception raised.*/public void AfterThrowingAdvice(IllegalArgumentException ex){System.out.println("There has been an exception: " + ex.toString());   }
}

Beans.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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><aop:config><aop:aspect id="log" ref="logging"><aop:pointcut id="selectAll" expression="execution(* com.tutorialspoint.*.*(..))"/><aop:before pointcut-ref="selectAll" method="beforeAdvice"/><aop:after pointcut-ref="selectAll" method="afterAdvice"/><aop:after-returning pointcut-ref="selectAll" returning="retVal"method="afterReturningAdvice"/><aop:after-throwing pointcut-ref="selectAll" throwing="ex"method="AfterThrowingAdvice"/></aop:aspect></aop:config><!-- Definition for student bean --><bean id="student" class="com.sap.Student"><property name="name"  value="Zara" /><property name="age"  value="11"/>      </bean><!-- Definition for logging aspect --><bean id="logging" class="com.sap.Logging"/> </beans>

MainApp.java:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {public static void main(String[] args) {try {ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");Student student = (Student) context.getBean("student");student.getName();student.getAge();      student.printThrowException();}catch (Exception e) {System.out.println("Jerry ends here!");}}
}

输出:

Jul 26, 2020 11:13:45 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b80350b: startup date [Sun Jul 26 11:13:45 CST 2020]; root of context hierarchy
Jul 26, 2020 11:13:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Going to setup student profile.
Name : Zara
Student profile has been setup.
Returning:Zara
Going to setup student profile.
Age : 11
Student profile has been setup.
Returning:11
Going to setup student profile.
Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
Jerry ends here!

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Spring 中基于 AOP 的 XML操作方式相关推荐

  1. spring中基于XML的AOP配置步骤

    spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...

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

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

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

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

  4. Spring中的AOP(三)——基于Annotation的配置方式(一)

    为什么80%的码农都做不了架构师?>>>    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是 ...

  5. Spring 中的AOP的通知类型的示例(xml)

    个人博客:https://suveng.github.io/blog/​​​​​​​ Spring 中的AOP的通知类型的示例 AOP中的通知类型(advice)一共有五中: around advic ...

  6. spring 加载java类_在Spring中基于Java类进行配置的完整步骤

    在Spring中基于Java类进行配置的完整步骤 发布于 2020-7-7| 复制链接 基于Java配置选项,可以编写大多数的Spring不用配置XML,下面 前言JavaConfig 原来是 Spr ...

  7. 一文读懂Spring中的AOP机制

    一.前言 这一篇我们来说一下 Spring 中的 AOP 机制,为啥说完注解的原理然后又要说 AOP 机制呢? 1.标记日志打印的自定义注解 @Target({ElementType.METHOD}) ...

  8. 动态代理——》AOP —— Spring 中的 AOP||AOP 相关术语||学习 spring 中的 AOP 要明确的事

    AOP 概述 什么是 AOP       AOP:全称是 Aspect Oriented Programming 即:面向切面编程 AOP 的作用及优势 作用: 在程序运行期间,不修改源码对已有方法进 ...

  9. spring中使用注解代替xml配置

    今天两部分内容,第一部分是注解,使用注解配置Spring,然后第二个是Spring中的AOP,今天就需要这两部分,也没有练习,第一个注解配置Spring,这三大框架都是用注解来配置,这三大框架,都是支 ...

最新文章

  1. 动态创建DeepZoom
  2. Py之utils:utils库的简介、安装、使用方法之详细攻略
  3. Mozilla “Common Voice” 开源语音识别项目
  4. Integer的127与128
  5. vs2015项目导出为Qt项目
  6. 大数据之-Hadoop3.x_Hadoop_MapReduce_介绍---大数据之hadoop3.x工作笔记0081
  7. 宏图之下服务器维护,《鸿图之下》3月24日维护更新预告
  8. 正则化与数据先验分布的关系
  9. 微信小程序java后端_微信小程序访问后端服务器-微信小程序后端-微信小程序后端JAVA...
  10. 生产质量分析,助力企业掌握影响质量的全量数据
  11. Excel科学计数法转换成文本完整显示
  12. 建筑建模学习笔记3——Vray渲染及PS修图
  13. 加速R运算的简易方法-MRO
  14. r语言alasso的系数怎么看_R语言用msgps包做adaptive lasso,系数怎么提取啊?
  15. js实现图片放大预览(jq+css)
  16. 带你详细了解 Android Lifecycle
  17. 百度、高德离线地图SDK开发工具,局域网内离线地图开发环境
  18. egg开发笔记(五)egg使用egg-sequelize需要注意的事项
  19. 我认识的林家翘先生---致敬伟大的我国数学家
  20. delay和sleep的区别

热门文章

  1. 设计模式(命令模式)
  2. 设计模式(二)之装饰器模式
  3. Knockout中ViewModel与Model的互转
  4. 分享Silverlight/WPF/Windows Phone一周学习导读(06月13日-06月18日)
  5. c# 扩展方法奇思妙用高级篇一:改进 Scottgu 的 In 扩展
  6. echars 在vue v-if 切换会 显示不出来或者显示出来但是不是百分百显示
  7. windows 下 配置 github
  8. P2038 无线网络发射器选址
  9. SQL学习之使用视图
  10. ubuntu登陆后一闪回到登陆界面