Spring的组成
Spring由20个核心依赖组成,这20个核心依赖可以分为6个核心模块
本篇文章着重描述Spring核心容器模块,其中包含了spring-beans,spring-core,spring-context,spring-expression-language(SpEl),spring-context-support等依赖
1. Spring核心容器
1.1 Spring核心容器的依赖简介
Spring Core Container 是 Spring 框架的核心部分,它为整个 Spring 框架提供了基础设施和基本功能。Spring Core Container 负责管理 Spring 应用中的所有组件(如 Beans)的创建、配置和生命周期,并通过依赖注入(Dependency Injection, DI)和面向切面编程(Aspect-Oriented Programming, AOP)等机制来促进松散耦合的设计。
Spring Core
核心功能模块:Spring Core 模块是整个 Spring 框架的基础,包含了依赖注入(DI)机制的实现。它定义了 Spring 的基础类和接口,如 BeanFactory 和 ApplicationContext,这些都是 Spring 应用的核心部分。
BeanFactory:BeanFactory 是 Spring 的核心接口,用于管理 Bean 的创建和配置。它提供了基本的 IoC(控制反转)功能,是所有 Spring 应用的基石。
ApplicationContext:ApplicationContext 是 BeanFactory 的一个扩展,它提供了更多的企业级功能,如事件传播、国际化、AOP 支持等。ApplicationContext 是 Spring 应用中的高级容器,用于更复杂的配置和管理。
Spring Beans
Bean 配置和管理:Spring Beans 模块用于管理和配置 Spring 应用中的 Bean。Bean 是 Spring 容器管理的对象,可以通过 XML、注解或 Java 配置类来定义和配置。
依赖注入:通过依赖注入机制,Spring 容器会自动将需要的依赖注入到 Bean 中,从而减少组件之间的耦合,使得代码更易于测试和维护。
Spring Context
ApplicationContext 扩展:Spring Context 模块是对 Core 和 Beans 模块的扩展,提供了更全面的框架功能。ApplicationContext 作为高级容器,在大型企业应用中非常常用。
注解驱动开发:Spring Context 支持注解驱动的依赖注入,可以通过注解如 @Component、@Service、@Repository 等来标识 Spring 管理的 Bean,并使用 @Autowired 来自动注入依赖。
Spring Expression Language (SpEL)
表达式语言支持:SpEL 是 Spring 提供的一种强大的表达式语言,可以在 XML 配置、注解或其他 Spring 配置中使用。SpEL 支持调用方法、访问属性、操作集合等,提供了极大的灵活性。
1.2 Spring核心容器的pom依赖
<!--Spring核心容器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
spring-core
Spring Core 是 Spring 框架的核心模块,提供了所有其他 Spring 模块
的基础功能。
1. 核心工具类:
StringUtils:处理字符串的工具类。
ObjectUtils:提供对象相关的工具方法。
ClassUtils:用于处理类加载和反射的实用方法。
CollectionUtils:用于处理集合的工具类。
Assert:提供常用的断言方法,方便进行前置条件的验证。
这些工具类在整个 Spring 框架和开发中广泛使用,提供了对基本操作的简化。
2. 资源抽象(Resource Abstraction):
Resource 接口 以及相关实现类(如 ClassPathResource、FileSystemResource、UrlResource 等),提供了一套统一的 API 来访问不同类型的资源(例如文件系统中的文件、类路径资源、URL 资源等)。
这种抽象使得应用程序可以轻松地在不同的资源位置之间切换,而不必修改业务逻辑。
3. 类型转换系统(Type Conversion System):
ConversionService 接口 提供了对类型转换的支持,允许在应用中轻松地进行类型转换。这在处理配置文件中的属性值或者注入时尤为重要。
还包括 PropertyEditor 支持,允许在需要时将字符串转换为复杂对象。
4. 环境抽象(Environment Abstraction):
Environment 接口 及其实现类,提供了对环境变量、系统属性和外部化配置的访问和管理。
PropertySource 和 PropertyResolver 接口帮助解析和管理配置属性,这些功能对于支持外部化配置和环境感知的应用程序至关重要。
5. 日志支持:
提供了对多种日志框架(如 Log4j、SLF4J)的抽象支持,允许应用程序在不同的日志实现之间切换而无需更改代码。
这里需要重点说明:spring-core本身并不提供对多种日志框架的支持,而是通过引入spring-jcl完成了这个功能;另外我们观察spring-jcl包的结构
包名居然是apache.commons.logging!这是因为spring团队对common.logging 进行了重写,所以包名还保留之前的apache.commons.logging
JCL的全称为: Jakarta commons-logging, 是apache公司提供的一个抽象的日志框架, 并不提供日志功能,若需要使用具体的日志则需要添加依赖的jar包,但是它没有维护了,所以spring团队自己重写推出了一个spring-jcl
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.2.RELEASE</version>
<name>Spring Core</name>
<description>Spring Core</description>
<url>https://github.com/spring-projects/spring-framework</url>
<organization>
<name>Spring IO</name>
<url>http://projects.spring.io/spring-framework</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>jhoeller</id>
<name>Juergen Hoeller</name>
<email>jhoeller@pivotal.io</email>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/spring-projects/spring-framework</connection>
<developerConnection>scm:git:git://github.com/spring-projects/spring-framework</developerConnection>
<url>https://github.com/spring-projects/spring-framework</url>
</scm>
<issueManagement>
<system>Jira</system>
<url>https://jira.springsource.org/browse/SPR</url>
</issueManagement>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.1.6</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>1.3.4</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava-reactive-streams</artifactId>
<version>1.2.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.sf.jopt-simple</groupId>
<artifactId>jopt-simple</artifactId>
<version>5.0.4</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
<scope>compile</scope>
<optional>true</optional>