Spring 框架
Spring 框架是最流行的 Java EE 框架之一。在本文中,我们将了解以下内容:
- Spring 框架架构
- Spring 核心组件
- 在 Spring 框架之下的各种项目。
我查看了 Spring Framework、Servlet、JSP 和 JSF 的 Google 趋势图。很明显,Spring 远远领先于所有其他 Java EE 技术。
什么是 Spring 框架?
Spring 是 Java 企业版最流行的框架之一。世界各地的开发人员都使用 Spring 来开发可靠且高质量的应用程序。Spring 框架由 Rod Johnson 设计。从那时起,Spring 就成为 Java 世界中 EJB 模型的替代技术。您可以使用 Spring 框架创建不同类型的应用程序。
Spring 核心概念-DI、IoC、AOP
- 如果不理解什么是依赖注入和控制反转,就不可能理解什么是 Spring 框架。依赖注入也称为 DI,是控制反转 (IoC) 的类型之一。
- 控制反转- 这是面向对象编程的原则,其中程序的对象不依赖于其他对象的具体实现,但可能了解它们的抽象(接口)以便进行以后的交互。
- 依赖注入- 是结构设计模式的组合,其中应用程序的每个功能都有一个条件独立的对象(服务),该对象可能需要使用通过接口已知的其他对象(依赖项)。依赖项在创建服务时转移(实现)到服务。在这种情况下,我们将一个类的元素引入另一个类。实际上,DI 是通过将参数传递给构造函数或使用 setter 来实现的。实现这种方法的库也称为 IoC 容器。
- 面向方面编程- 一种编程范式,可让您区分应用程序中的跨(功能)功能。这些跨越多个应用程序节点的功能称为横切关注点,这些横切关注点与应用程序的直接业务逻辑分离。在 OOP 中,关键单元是类,而在 AOP 中,关键元素是方面。DI 有助于将应用程序类分离为单独的模块,而 AOP 有助于将横切关注点与它们影响的对象分离。
Spring 框架架构
Spring 框架分为多个独立的模块,您可以决定在应用程序中使用哪些模块。下图说明了 Spring 框架架构中最重要的模块。让我们逐一了解 Spring 生态系统模块。
Spring 框架核心组件
The Core container from Spring consists of four modules: SpEL , Context, Core, Beans. Description for these elements are as follows:
- The SpEL module provides a powerful expression language for manipulating objects during execution.
- Context is built on the basis of Beans and Core and allows you to access any object that is defined in the settings. The key element of the Context module is the ApplicationContext interface.
- The Core module provides key parts of the framework including IoC and DI properties.
- The Bean module is responsible for creating and managing Spring Beans - is application context structure unit.
Spring Framework Web
Spring framework Web layer consists of Web, Web-MVC, Web-Socket, Web-Portlet etc.
- The Web module provides functions such as downloading files, creating web application, rest web service etc.
- Web-MVC contains a Spring MVC implementation for web applications.
- Web-Socket provides support for communication between the client and the server, using Web-Sockets in web applications.
- Web-Portlet provides MVC implementation with portlet environment
Spring Framework Data Access
The Data Access/Integration container consists of JDBC, ORM, OXM, JMS and the Transactions module.
- JDBC provides an abstract layer of JDBC and eliminates the need for the developer to manually register the monotonous code associated with connecting to the database.
- Spring ORM provides integration with popular ORMs such as Hibernate, JDO, which are implementations of JPA.
- The OXM module is responsible for linking the Object / XML - XMLBeans, JAXB, etc.
- The JMS (Java Messaging Service) module is responsible for creating, sending and receiving messages.
- Transactions supports transaction management for classes that implement certain methods and POJOs.
Miscellaneous Modules
Spring also includes a number of other important modules, such as AOP, Aspects, Instrumentation, Messaging, and Test.
- AOP implements aspect-oriented programming and allows using the entire arsenal of AOP capabilities.
- The Aspects module provides integration with AspectJ, which is also a powerful AOP framework.
- Instrumentation is responsible for supporting class instrumentation and class loader, which are used in server applications.
- The Messaging module provides STOMP support.
- Finally, the Test module provides testing using TestNG or the JUnit Framework.
Spring 5 Features
Spring 5 brought massive update to Spring 4. Some of the important features of Spring 5 are:
- Support for Java 8, Java 9, Java EE 7, Java EE 8, Servlet 4.0, Bean Validation 2.0, and JPA 2.2. I am happy to see that Spring is trying to catch up with latest versions of major technologies being used.
- Improved Logging with new module - spring-jcl.
- File operations are using NIO 2 streams, hence improved performance.
- Support for Reactor 3.1 Flux and Mono as well as RxJava 1.3 and 2.1 as return values from Spring MVC controller methods.
- 支持 Kotlin、Project Lombok、JSON Binding API 作为 Jackson 和 GSON 的替代。
- Spring WebFlux ——Spring 变得反应灵敏。
- 支持JUnit 5
- 通过 Kotlin 支持函数式编程。
您可以在我们的GitHub 存储库中查看 Spring 项目。
参考资料:Spring官网、Spring参考文档