【工具】Spring 历史官方文档理解(持续更新)

news2024/10/6 14:24:40

文章目录

    • [1] Spring Framework 5.2.24
      • Core
        • AOP 概念
          • Aspect
          • Join point
          • Advice
          • Pointcut
          • Introduction
          • Target object
          • AOP proxy
          • Weaving
        • Spring AOP
        • @AspectJ
        • 官方 demo 学习 Pointcut 表达式
        • 官方 demo 学习 Advice 声明
        • 官方 demo 学习 Introductions (接口拓展)
        • AOP 实现形式
        • AOP 与 IOC 协作

[1] Spring Framework 5.2.24

  • 官方文档入口
  • 查询方式
    在这里插入图片描述

Core

Core 的内容主要包括 IOC 和 AOP,Spring 有自己的 AOP 框架,能够解决 80% 的企业级应用的需求。(如果不够用)同时也提供集成 AspectJ 的方案。

原文:Foremost amongst these is the Spring Framework’s Inversion of Control (IoC) container. A thorough treatment of the Spring Framework’s IoC container is closely followed by comprehensive coverage of Spring’s Aspect-Oriented Programming (AOP) technologies. The Spring Framework has its own AOP framework, which is conceptually easy to understand and which successfully addresses the 80% sweet spot of AOP requirements in Java enterprise programming.
Coverage of Spring’s integration with AspectJ (currently the richest — in terms of features — and certainly most mature AOP implementation in the Java enterprise space) is also provided.Coverage of Spring’s integration with AspectJ (currently the richest — in terms of features — and certainly most mature AOP implementation in the Java enterprise space) is also provided.

AOP 概念

AOP的术语不直观,但是为了避免术语混乱, Spring 也没有自己维护一套术语。这里整理下术语的概念。

原文
These terms are not Spring-specific. Unfortunately, AOP terminology is not particularly intuitive. However, it would be even more confusing if Spring used its own terminology.

Aspect

形如 Transaction management ,在企业项目中,Transaction management 作为一个 Aspect 可以 跨多个类 对事务进行管理

原文:A modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented by using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).

Join point

概念上有2种形式,在 Spring 中,往往指的是程序执行的链条中的某个方法

原文:A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

Advice

这个概念,融合了上面两个名词。指的是在 Aspect 中某个 Join point 上执行的具体逻辑。有三种Advice 类型

  • Before
    • Join point 前执行
    • 除了抛异常,无法中断
  • After returning
    • 正常结束会调用
  • After throwing
    • 抛异常时会调用
  • After (finally)
    • 无论如何(正常、抛异常)都会被调用
  • Around
    • 有着Before After 的能力
    • 能够修改返回值
    • 能够抛出指定异常

原文:Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

Spring 把 Advice 建模成 Interceptor。围绕一个Join point,可以建立多个 interceptor
这里有疑问:Spring MVC 中同时存在 Advice 和 Interceptor 结尾的类,他们是不是都是一个意思?

原文:Action taken by an aspect at a particular join point. Different types of advice include “around”, “before” and “after” advice. (Advice types are discussed later.) Many AOP frameworks, including Spring, model an advice as an interceptor and maintain a chain of interceptors around the join point.

Pointcut

用于路由到 Join point (某个方法) 的具体规则,规则的描述默认使用
AspectJ pointcut expression

原文: A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

Introduction

被而外添加到(类)中的方法或字段

原文:Declaring additional methods or fields on behalf of a type. Spring AOP lets you introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

Target object

被一个 Aspect 或多个 Aspect 增强的类。Spring 常常使用运行时的代理获取被增强的类。

AOP proxy

动态代理的实现,可以是JDK dynamic proxy 或者 CGLIB proxy

Weaving

编译时期、类装载时、运行时;根据目标对象产生增强的对象,这个过程就是 Weaving。Spring 在运行时进行 Weaving

原文:linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

Spring AOP

与 AspectJ 的关系:

  • Spring AOP 不支持 field 的拦截,如果有这个需要可以考虑使用 AspectJ

原文:Field interception is not implemented, although support for field interception could be added without breaking the core Spring AOP APIs. If you need to advise field access and update join points, consider a language such as AspectJ.

  • Spring AOP 与 AspectJ 兼容(都通过IOC集成),可以任意组合使用

原文: Spring seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API. Spring AOP remains backward-compatible.

@AspectJ

相关链接 >>

官方 demo 学习 Pointcut 表达式

@Aspect
public class CommonPointcuts {

    /**
     * 切  com.xyz.myapp.service 包下的所有方法
     */
    @Pointcut("within(com.xyz.myapp.service..*)")
    public void inServiceLayer() {}

    /**
     * 接口在 com.xyz.myapp.abc.service 
     * 和 com.xyz.myapp.def.service 中 
     * 且实现类都在对应的 service (子)包下, 
     * 官网这里说的意思跟是否实现了接口无关,
     * 只要关注service.*.子包的内容即可
     */
    @Pointcut("execution(* com.xyz.myapp..service.*.*(..))")
    public void businessService() {}
}

更多例子 >>

官方 demo 学习 Advice 声明

结合 Pointcut 表达式使用:

@Aspect
public class AfterReturningExample {

	// Advice 上使用 Pointcut 表达式
    @AfterReturning(
        pointcut="com.xyz.myapp.CommonPointcuts.dataAccessOperation()",
        returning="retVal")
    public void doAccessCheck(Object retVal) {
        // ...
    }
}
  • 环绕通知,比较全面的使用切面
@Aspect
public class AroundExample {

    @Around("com.xyz.myapp.CommonPointcuts.businessService()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        // start stopwatch
        Object retVal = pjp.proceed();
        // stop stopwatch
        return retVal;
    }
}
  • 泛型支持
public interface Sample<T> {
    void sampleGenericMethod(T param);
    void sampleGenericCollectionMethod(Collection<T> param);
}

官方 demo 学习 Introductions (接口拓展)

@Aspect
public class UsageTracking {

	// 切 UsageTracked 接口的实现类,默认实现是 DefaultUsageTracked
    @DeclareParents(value="com.xzy.myapp.service.*+", defaultImpl=DefaultUsageTracked.class)
    public static UsageTracked mixin;

    @Before("com.xyz.myapp.CommonPointcuts.businessService() && this(usageTracked)")
    public void recordUsage(UsageTracked usageTracked) {
        usageTracked.incrementUseCount();
    }

}

AOP 实现形式

  • Schema-based (类似于xml的配置文件形式)

  • @AspectJ-based(AspectJ 注解形式)

  • lower-level AOP(底层编程形式)
    详情 >>

AOP 与 IOC 协作

简单来说,就是先把bean用AOP增强,再加入到IOC容器

  • 怎么介入 IOC 的收集工作
public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {

    // simply return the instantiated bean as-is
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
    	// 可以在这里返回一个新的实例给IOC容器
    	// 注意,这里是所有bean初始化都会调这个方法
    	// 所以可以用自定义注解进行隔离(只操作自己关心的)
        return bean; 
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println("Bean '" + beanName + "' created : " + bean.toString());
        return bean;
    }
}
  • 利用 BeanPostProcessor 的实现,把指定名称的bean加入到IOC容器中
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames" value="jdk*,onlyJdk"/>
    <property name="interceptorNames">
        <list>
            <value>myInterceptor</value>
        </list>
    </property>
</bean>

原文:The BeanNameAutoProxyCreator class is a BeanPostProcessor that automatically creates AOP proxies for beans with names that match literal values or wildcards. The following example shows how to create a BeanNameAutoProxyCreator bean:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/680028.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

0004Java程序设计-SSM+JSP医院挂号系统

摘 要 医院挂号&#xff0c;一直以来就是困扰医院提高服务水平的重要环节&#xff0c;特别是医疗水平高、门诊访问量高的综合型医院&#xff0c;门诊拥挤就成了普遍现象。因此&#xff0c;本文提出了医院挂号系统。预约挂号&#xff0c;是借助信息化的技术&#xff0c;面向全社…

代码随想录二刷 day32 | 贪心之 122.买卖股票的最佳时机II 55. 跳跃游戏 45.跳跃游戏II

这里写目录标题 122.买卖股票的最佳时机II55. 跳跃游戏45.跳跃游戏II 122.买卖股票的最佳时机II 题目链接 解题思路&#xff1a; 首先要清楚两点&#xff1a; 只有一只股票&#xff01;当前只有买股票或者卖股票的操作 想获得利润至少要两天为一个交易单元。 代码如下&#x…

【Unity每日一记】时间Time类-做时间管理大师

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;uni…

独立开发变现周刊(第92期):创建一个年收入350万美元的小工具,1000万至1500万美元出售...

分享独立开发、产品变现相关内容&#xff0c;每周五发布。 目录 1、Vercel AI: 使用React, Svelte和Vue快速构建 AI 驱动的应用2、Novel&#xff1a;AI自动补全功能的Notion风格所见即所得编辑器3、Notionbase: 通过Notion轻松建立你的AI聊天机器人4、Plasmo: 一款功能强大的浏…

Python入门教程+项目实战-14.1节-程序实战-二分查找算法

目录 14.1.1 理解函数类型 14.1.2 函数的定义 14.1.3 函数的形参&#xff0c;实参&#xff0c;以及调用 14.1.4 函数的返回值 14.1.5 函数的命名规范 14.1.6 知识要点 14.1.7 系统学习python 14.1.1 理解函数类型 在Python中&#xff0c;函数也是一种数据类型。在理解函…

C++——详解类模板与友元函数

纵有疾风起&#xff0c;人生不言弃。本文篇幅较长&#xff0c;如有错误请不吝赐教&#xff0c;感谢支持。 &#x1f4ac;文章目录 类模板与友元函数1️⃣非模板友元函数2️⃣约束模板友元函数3️⃣非约束模板友元函数 类模板与友元函数 模板类的友元函数有三类&#xff1a; …

Qt/C++编写手机版本视频播放器和Onvif工具(可云台和录像)

一、前言 用Qtffmpeg写播放器很多人有疑问&#xff0c;为何不用Qt自己的多媒体框架来写&#xff0c;最重要的原因是Qt自带的目前都依赖具体的本地解码器&#xff0c;如果解码器不支持&#xff0c;那就是歇菜的&#xff0c;最多支持个MP4格式&#xff0c;而且在手机上也都是支持…

C国演义 [第七章]

第七章 最长重复子数组题目理解步骤dp含义递推公式初始化为啥dp数组如此奇怪 遍历顺序 代码 最长公共子序列题目理解步骤dp含义递推公式初始化遍历顺序 代码 总结 最长重复子数组 力扣链接 给两个整数数组 nums1 和 nums2 &#xff0c;返回 两个数组中 公共的 、长度最长的子…

设计模式之中介者模式笔记

设计模式之中介者模式笔记 说明Mediator(中介者)目录中介者模式示例类图抽象中介者类抽象同事类租房者类房主类具体的中介者角色类测试类 说明 记录下学习设计模式-中介者模式的写法。JDK使用版本为1.8版本。 Mediator(中介者) 意图:用一个中介对象来封装一系列的对象交互。…

剑指 Offer 68 - II. 二叉树的最近公共祖先 / LeetCode 236. 二叉树的最近公共祖先(搜索与回溯)

题目&#xff1a; 链接&#xff1a;剑指 Offer 68 - II. 二叉树的最近公共祖先&#xff1b;LeetCode 236. 二叉树的最近公共祖先 难度&#xff1a;中等 上一题博客&#xff1a;剑指 Offer 68 - I. 二叉搜索树的最近公共祖先 / LeetCode 235. 二叉搜索树的最近公共祖先&#xf…

python:并发编程(二十四)

前言 本文将和大家一起探讨python并发编程的实际项目&#xff1a;win图形界面应用&#xff08;篇六&#xff0c;共八篇&#xff09;&#xff0c;系列文章将会从零开始构建项目&#xff0c;并逐渐完善项目&#xff0c;最终将项目打造成适用于高并发场景的应用。 本文为python并…

Pandas进阶修炼120题-第二期(Pandas数据处理,21-50题)

文章目录 Pandas进阶修炼120题第二期 Pandas数据处理21.读取本地EXCEL数据22.查看df数据前5行23.将salary列数据转换为最大值与最小值的平均值方法一&#xff1a;正则表达式&#xff08;分别使用apply()&#xff0c;applymap(),map()来实现&#xff09;方法二&#xff1a;apply…

wifi芯片原理

一、在系统中的位置 基带&#xff08;baseband&#xff09; 基带的作用有三个&#xff1a; 1、队列包的管理 2、调试解调 3、CSMA/CA机制 CSMA/CA的全称是Carrier Sense Multiple Access with Collision Avoidance&#xff0c;即载波侦听多路访问&#xff0f;冲突避免。 各…

java——集合框架

文章目录 接口实现&#xff08;类&#xff09;算法1. 排序算法2. 查找算法3. 拷贝算法4. 填充算法5. 比较算法6. 随机算法7. 迭代器算法8. 交集、并集、差集9. 分割集合10. 数组和集合的互转 集合框架是一个用来代表和操纵集合的统一架构。所有的集合框架都包含如下内容&#x…

C++手撕红黑树

目录&#xff1a; 红黑树的概念红黑树的性质红黑树节点的定义 红黑树结构红黑树的插入操作红黑树的验证红黑树的代码实现 红黑树的删除红黑树与AVL树的比较红黑树的应用 总结 红黑树的概念 红黑树&#xff0c;是一种二叉搜索树&#xff0c;但在每个结点上增加一个存储位表示结…

DevOps系列文章之 Spring Boot Docker打包

应用准备容器化&#xff0c;因为几十个应用从测试到发布太麻烦了&#xff0c;而且还会因为环境的因素导致部署中出现各种问题。为了在开发、测试、生产都能保持一致的环境&#xff0c;就引进了容器技术&#xff0c;而目前常用的应用使用基于spring boot的。 在Spring Boot应用…

AI数字人之语音驱动面部模型及超分辨率重建Wav2Lip-HD

1 Wav2Lip-HD项目介绍 数字人打造中语音驱动人脸和超分辨率重建两种必备的模型&#xff0c;它们被用于实现数字人的语音和图像方面的功能。通过Wav2Lip-HD项目可以快速使用这两种模型&#xff0c;完成高清数字人形象的打造。 项目代码地址&#xff1a;github地址 1.1…

Canal监听MySQL

Canal监听MySQL 1、Mysql数据库开启binlog模式 注意&#xff1a;Mysql容器&#xff0c;此处Mysql版本为5.7 #进入容器 docker exec -it mysql /bin/bash #进入配置目录 cd /etc/mysql/mysql.conf.d #修改配置文件 vi mysqld.cnf(1) 修改mysqld.cnf配置文件&#xff0c;添加如…

Android:安卓开发使用okHttp进行网络请求和MySQL数据库完成图书馆管理系统APP

1、总体目标 1.1 项目概述 项目名称&#xff1a;基于安卓平台的图书管理系统。 本项目旨在研发一个图书管理系统&#xff0c;实现图书馆的信息化管理。在方便用户在线浏览&#xff0c;借阅&#xff0c;归还图书&#xff0c;方便图书馆管理员对图书进行管理。能很好的为用户提…

从零开始理解Linux中断架构(7)--- Linux执行上下文之中断上下文

1 中断处理程序的基本要求 当前运行的loop是一条执行流,中断程序运行开启了另外一条执行流,从上一节得知这是三种跳转的第三类,这个是一个大跳转。对中断程序的基本要求就是中断执行完毕后要恢复到原来执行的程序,除了时间流逝外,原来运行的程序应该毫无感知。 具体到Armv…