Spring 框架介绍和使用

182 阅读6分钟

历史的选择

Spring 作为一个基础的框架,是在 Java EE 开发历史中,是成千上万公司选择。单独使用 Spring 的非常少了,很多都是用 Spring-Boot/Spring-Cloud 来开发,但是 Spring 基础依然是我们使用的基石。我们将一起来聊一聊 Spring 的基本使用。首先我们一起来了解一下 Spring 框架整体架构图如下:图片

  1. 数据访问/集成,包括 JDBC 、ORM、OXM、JMS 和 Transaction 模块;
  2. WEB 模块,包括 WebSocket、Servlet、Web、Porlet 模块;
  3. 核心容器,包括 Bean 模块、Core 模块、Context 模块 和 SpEL 模块;
  4. 其他部分,包括:AOP、Test 等模块

Spring 同类框架

  • Micronaut
  • Quarkus

Spring 核心功能

核心功能:控制反转(IOC) 、AOP 非核心功能:事件驱动、国际化、资源管理,数据绑定、类型转换 、SpEL、单元测试等。

PS:核心功能,在本文会有使用实践。

Spring Bean 容器

控制反转(IOC)是 Spring 框架的核心功能之一,其本质的就是将用户创建 Bean 的过程赋予给 IOC 容器去完成,实现 Bean 创建权利的反转为容器来创建 Bean 和依赖 Bean 。

image.png

Bean 创建

Spring 容器创建 Bean 只需要三个步骤:

  1. 定义 Bean
  2. 创建 Bean 容器/Bean 工厂
  3. 获取 Bean 对象

举一个例子:

<span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">TestMain</span> </span>{<br style="box-sizing: border-box;"><br style="box-sizing: border-box;">    <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">static</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">void</span> <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">main</span><span style="box-sizing: border-box;line-height: 26px;">(String[] args)</span> </span>{<br style="box-sizing: border-box;">        ApplicationContext applicationContext =<br style="box-sizing: border-box;">                <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">new</span> AnnotationConfigApplicationContext(AppConfig<span style="box-sizing: border-box;line-height: 26px;">.<span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span>)</span>;<br style="box-sizing: border-box;">        Student student = applicationContext.getBean(Student<span style="box-sizing: border-box;line-height: 26px;">.<span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span>)</span>;<br style="box-sizing: border-box;">        student.study();<br style="box-sizing: border-box;">        student.sleep();<br style="box-sizing: border-box;">        student.study();<br style="box-sizing: border-box;">    }<br style="box-sizing: border-box;">}<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Configuration</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Import</span>(Student<span style="box-sizing: border-box;line-height: 26px;">.<span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span>)<br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">AppConfig</span> </span>{<br style="box-sizing: border-box;"><br style="box-sizing: border-box;">}<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Component</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">Student</span> </span>{<br style="box-sizing: border-box;"><br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">private</span> String name;<br style="box-sizing: border-box;"><br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">private</span> Integer source;<br style="box-sizing: border-box;"><br style="box-sizing: border-box;">    <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">void</span> <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">study</span><span style="box-sizing: border-box;line-height: 26px;">()</span> </span>{<br style="box-sizing: border-box;">        System.out.println(<span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"学习中..."</span>);<br style="box-sizing: border-box;">    }<br style="box-sizing: border-box;"><br style="box-sizing: border-box;">    <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">void</span> <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">sleep</span><span style="box-sizing: border-box;line-height: 26px;">()</span> </span>{<br style="box-sizing: border-box;">        System.out.println(<span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"休息中..."</span>);<br style="box-sizing: border-box;">    }<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// setter getter</span><br style="box-sizing: border-box;">}<br style="box-sizing: border-box;">

运行上面的代码我们可以得到一下结果:

学习中...<br style="box-sizing: border-box;">休息中...<br style="box-sizing: border-box;">学习中...<br style="box-sizing: border-box;">

上面的代码执行什么呢?其实我们可以将 ApplicationContext理解为 Spring 容器对象,然后我们在 AppConfig 配置类中去定义 Spring 容器去帮助我们加载那些 Bean ,最后我们通过 getBean 方法获取我们注册的 Bean 对象。如下图:在这个过程中使用到那些关键的接口/类呢?

  • BeanFactory� 是 Bean 的抽象工厂,也就是我们 ApplicationContext 的一个父接口。
  • BeanDefinition� 是 Bean 的定义信息, 比如 beanName, className, isAbstract� 等 Bean 定义信息。

注入依赖 (DI)

Spring IOC 容器主要是解决了 Bean 的创建和依赖管理的问题。我们常见的有两种依赖注入方式:

  1. 属性注入
  2. 构造方法注入

属性注入

通过成员属性的方式实现 Bean 的自动注入

<span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Component</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">Student</span> </span>{<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Autowired</span><br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">private</span> Address address;<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// ...</span><br style="box-sizing: border-box;">}<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Component</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">Address</span> </span>{<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// ...</span><br style="box-sizing: border-box;">}<br style="box-sizing: border-box;">
  • 通过 @Component可以将 Student 、Address 类标记为一个 bean 对象
  • 通过 @Autowired可以将依赖 Bean 自动注入进来。

构造方法注入

通过构造方法实现 Bean 的自动注入

<span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Component</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">Student</span> </span>{<br style="box-sizing: border-box;">    <br style="box-sizing: border-box;"> <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">Student</span><span style="box-sizing: border-box;line-height: 26px;">(Address address)</span> </span>{<br style="box-sizing: border-box;">        <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">this</span>.address = address;<br style="box-sizing: border-box;">    }<br style="box-sizing: border-box;">}<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Component</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">Address</span> </span>{<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// ...</span><br style="box-sizing: border-box;">}<br style="box-sizing: border-box;">

Spring 的 IOC 解决了什么问题?

  1. 容器化,Spring包含并管理应用中对象的生命周期和配置(配置成单例还是原型,以及什么时候使用什么时候销毁)。
  2. 方便解耦,简化开发,Spring就是一个大工厂,可以将所有对象创建和依赖关系维护交给Spring管理,实现松耦合。符合高内聚低耦合的思想,这个特性也叫IOC(控制反转)。
  3. AOP编程的支持,Spring提供面向切面的编程,可以方便的实现对程序进行权限拦截、运行监控等功能,是通过动态代理和CGlib实现的,底层原理是反射。
  4. 声明式事务的支持,通过AOP来实现。不需通过编程的方式而进行管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明,便可将事务规则应用到业务逻辑中。
  5. 方便程序的测试,Spring对Junit4的支持,可以通过注解方便的测试Spring程序。
  6. 方便集成各种优秀框架,Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架的直接支持(如Struts2、Hibernate、MyBatis等)。
  7. 异常处理,Spring提供方便的API把具体技术相关的异常转化为一致的unchecked异常(比如由JDBC、Hibernate或者JDO抛出的异常)。SpringMVC也有一个异常集中处理的思想,将异常抛给SpringMVC框架,由框架来处理异常。
  8. 降低JavaEE API的使用难度,Spring对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低。

Spring AOP 面向切面

AOP(Aspect Oriented Programming)是面向切面的意思。

理解 AOP

Java 是一个面向对象(OOP)的编程语言,但是它有一个弊端就是需要为多个不具有继承关系的对象引入一个公共行为时,例如:日志记录、权限验证、事务管理、访问统计等公共行为,这样不便于维护,而且有大量重复代码,AOP 可以实现和 AOP 的互补。

举个例子: 我们有两个逻辑登录业务、订单业务,需要在他们调用前后进行:权限验证、日志记录等公共逻辑。

  1. 通过 OOP 的方式实现我们需要做一个逻辑模板:权限验证,具体逻辑(登录、订单),日志记录。
  2. 通过 AOP 的方式实现我们只需针对具体逻辑(登录、订单)前后做一个自定义切点,进行权限验证、日志记录。

如下图: 经过 AOP 方式处理过后,我们可以减少公共对象的引用、通过非继承的方式来处理切入逻辑的拦截,实现公共逻辑和业务的逻辑的松耦合关系。

AOP 实现

Spring 通过代理的方式去实现 AOP,Java 代理的两种模式:静态代理、动态代理。

  • 静态代理:静态代理是指在程序运行前,可以理解为是 .java 文件编译后就存在代理类的字节码 .class 文件。
  • 动态代理:动态代理指在程序运行期间通过 JVM 反射等动态机制,在运行期生成代理对象确定代理逻辑。

Spring 的两种代理模式:

  • JDK 代理:核心类 JdkDynamicAopProxy。
  • GCLIB 代理:核心类 ObjenesisCglibAopProxy。

两种代理的选择:如果 Bean 实现了接口就采用 JDK 代理, 如果没有实现就采用 GCLIB 代理。

AOP 使用

假设已经有一个 UserService 类提供了登录业务,我们需要对该业务做一个【权限验证】、【日志记录】这两个公共逻辑,在不修改 UserService 类代码的前提下就可以通过 AOP 来解决。示例如下:

<span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// 1. 测试类</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">AopTest</span> </span>{<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">static</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">void</span> <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">main</span><span style="box-sizing: border-box;line-height: 26px;">(String[] args)</span> </span>{<br style="box-sizing: border-box;">        ApplicationContext applicationContext =<br style="box-sizing: border-box;">                <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">new</span> AnnotationConfigApplicationContext(AopConfig<span style="box-sizing: border-box;line-height: 26px;">.<span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span>)</span>;<br style="box-sizing: border-box;">        UserService userService = applicationContext.getBean(UserService<span style="box-sizing: border-box;line-height: 26px;">.<span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span>)</span>;<br style="box-sizing: border-box;">        userService.login(<span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"admin"</span>, <span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"123456"</span>);<br style="box-sizing: border-box;">    }<br style="box-sizing: border-box;">}<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// 2. 配置类</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@EnableAspectJAutoProxy</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Configuration</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Import</span>({UserService<span style="box-sizing: border-box;line-height: 26px;">.<span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span>, <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">ValidateAspect</span>.<span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">class</span>})<br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">AopConfig</span> </span>{<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><br style="box-sizing: border-box;">}<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// 业务类</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Component</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">UserService</span> </span>{<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> String <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">login</span><span style="box-sizing: border-box;line-height: 26px;">(String username, String password)</span> </span>{<br style="box-sizing: border-box;">        System.out.println(<span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"username:"</span> + username + <span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">",password:"</span> + password);<br style="box-sizing: border-box;">        <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">return</span> <span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"ok"</span>;<br style="box-sizing: border-box;">    }<br style="box-sizing: border-box;">}<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// Aspect</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Aspect</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Component</span><br style="box-sizing: border-box;"><span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">class</span> <span style="box-sizing: border-box;color: rgb(230, 192, 123);line-height: 26px;">ValidateAspect</span> </span>{<br style="box-sizing: border-box;"><br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Pointcut</span>(<span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"execution(public * io.zhengsh.simu.spring.UserService.*(..))"</span>)<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">void</span> <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">servicePoint</span><span style="box-sizing: border-box;line-height: 26px;">()</span> </span>{<br style="box-sizing: border-box;">        <span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">// Do nothing</span><br style="box-sizing: border-box;">    }<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><br style="box-sizing: border-box;">    <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">@Around</span>(<span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"servicePoint()"</span>)<br style="box-sizing: border-box;">    <span style="box-sizing: border-box;line-height: 26px;"><span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">public</span> Object <span style="box-sizing: border-box;color: rgb(97, 174, 238);line-height: 26px;">doAroundService</span><span style="box-sizing: border-box;line-height: 26px;">(ProceedingJoinPoint joinPoint)</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">throws</span> Throwable </span>{<br style="box-sizing: border-box;">        System.out.println(<span style="box-sizing: border-box;color: rgb(152, 195, 121);line-height: 26px;">"validate param invoke !!!"</span>);<br style="box-sizing: border-box;">        <span style="box-sizing: border-box;color: rgb(198, 120, 221);line-height: 26px;">return</span> joinPoint.proceed();<br style="box-sizing: border-box;">    }<br style="box-sizing: border-box;">}<br style="box-sizing: border-box;"><br style="box-sizing: border-box;"><br style="box-sizing: border-box;">

maven 依赖

<span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"><!-- Spring核心依赖 --></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span>org.springframework<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span>spring-core<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span>${spring.version}<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"><!-- Spring beans包--></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span>org.springframework<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span>spring-beans<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span>${spring.version}<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"><!-- Spring 容器包 --></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span>org.springframework<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span>spring-context<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span>${spring.version}<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;"><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"><!-- aop --></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span>org.springframework<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span>spring-aop<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span>${spring.version}<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;"><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span>org.aspectj<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">groupId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span>aspectjweaver<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">artifactId</span>></span><br style="box-sizing: border-box;">      <span style="box-sizing: border-box;line-height: 26px;"><<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span>1.9.9.1<span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">version</span>></span><br style="box-sizing: border-box;">  <span style="box-sizing: border-box;line-height: 26px;"></<span style="box-sizing: border-box;color: rgb(224, 108, 117);line-height: 26px;">dependency</span>></span><br style="box-sizing: border-box;">

参考文档

  • Spring 官方文档
  • 骆驼整理说-Spring AOP
  • Java-为什么使用Spring框架