6.1 Java EE——Introduction to Spring

1. Overview of

The core technology of the String framework

        Spring is a layered Java SE/EE one-stop (full-stack) lightweight open source framework organized and developed by Rod Johnson. Its core concepts are IoC (Inversion of Control) and AOP (Aspect-Oriented Programming). Among them, IoC is the foundation of Spring, which supports Spring's management function for JavaBean; AOP is an important feature of Spring. AOP realizes program functions through pre-compilation and dynamic proxy during runtime, which means that functions can be added to programs uniformly without modifying the source code.

The role of String in the presentation layer, business logic layer and persistence layer

(1) In the presentation layer, it provides the Spring MVC framework, and Spring can also be integrated with the Struts framework.

(2) In the business logic layer, you can manage transactions, record logs, etc.

(3) MyBatis, Hibernate, JdbcTemplate and other technologies can be integrated in the persistence layer.

Two, the advantages of the Spring framework

The role of the String framework

        As an all-round application framework of Java EE, Spring provides a robust and efficient solution for developing enterprise-level applications. It can be applied not only to the development of Java applications, but also to server-side development. The reason why Spring is so widely used is because the Spring framework has the following advantages.

Advantages of the String framework:

a. Non-intrusive design

        Spring is a non-invasive (non-invasive) framework. The so-called non-invasive means that the API of the Spring framework will not appear in the business logic, that is to say, the business logic should be pure, and no code that has nothing to do with the business logic can appear. Since there is no Spring API in the business logic, the business logic code can also be quickly transplanted from the Spring framework to other frameworks.

b. Reduce coupling

        Spring is a big factory, which can hand over the creation of all objects and the maintenance of dependencies to the Spring container management, which greatly reduces the coupling between components.

c. Support AOP programming

        Spring provides support for AOP. AOP can centralize some common tasks, such as security, transactions and logs, to reduce code redundancy and complexity brought by traditional OOP methods.

d. Support declarative transactions

        In Spring, database transactions can be managed directly through Spring configuration files, which saves the tedious manual programming and improves development efficiency.

e. Convenient program testing

        Spring provides support for Junit, and developers can use Junit for unit testing.

f. Convenient integration framework

        Spring provides a broad basic platform, which provides direct support for various frameworks, such as Struts, Hibernate, MyBatis, Quartz, etc. These excellent frameworks can be seamlessly integrated with Spring.

g. Reduce the difficulty of using Java EE API

        Spring encapsulates some APIs in Java EE development (such as JDBC, JavaMail, etc.), which greatly reduces the difficulty of using these APIs.

3. The architecture of Spring

Spring Architecture Diagram

Spring 5 Framework Component Modules

        The Spring framework mainly has 8 major modules, and each major module is composed of multiple or 1 small module. For example, Spring's core container module (Core Container) is composed of Beans module, Core module, Context module and SpEL module. The following is a brief introduction to the main modules in the Spring architecture combined with the architecture diagram of String. 

Core Container Module (Core Container)

        The core container module plays a supporting role in Spring's functional system and is the cornerstone of other modules. The core container layer is mainly composed of Beans module, Core module, Context module and SpEL module.

The core container module consists of each module

        (1) Beans module. It provides the BeanFactory class, which is a classic implementation of the factory pattern. The main function of the Beans module is to create and manage Bean objects.

        (2) Core module. It provides the basic components of the Spring framework, including IoC and DI functionality.

    (3) Context module. It is built on the basis of the Beans module and the Core module, and it can provide context information through the ApplicationContext interface.

     (4) SpEL module. It is a new module added after Spring 3.0, which provides support for SpEL Expression Language (Spring Expression Language), which is an expression language that supports operating object graphs when the program is running.

b. Data Access and Integration Module (Data Access/Integration)

        The data access and integration module is used to access and operate the data in the database, which mainly includes JDBC module, ORM module, OXM module, JMS module and Transactions module.

Data access and integration module Composition of each module

        (1) JDBC module. It provides a JDBC abstraction layer that eliminates tedious JDBC coding and can parse database vendor-specific error codes.

        (2) ORM module. It provides an integration layer for mainstream object-relational mapping APIs for integrating mainstream object-relational mapping frameworks.

        (3) OXM module. It provides support for XML-mapped abstraction layers such as JAXB, Castor, etc.

        (4) JMS module. It is mainly used to pass messages, including the production and consumption of messages. Since version 4.1, the JMS module supports integration with the Spring-message module.

        (5) Transactions module. Its main function is transaction management.

c. Web module

The implementation of the Web module is based on APPlicationContext, which provides various tool classes for Web applications, including Web modules, Servlet modules, WebSocket modules and Portlet modules.

Each module of the Web module consists of

         (1) Web module. It provides integrated features for web development, such as most file upload functions. Additionally, the Web module contains an HTTP client and the Web-related parts of Spring's remoting support.

        (2) Servlet module. It provides Spring's model, view, controller, and REST web service implementations for web applications.

        (3) WebSocket module. It is a new module after Spring 4.0, which provides the implementation of WebSocket and SockJS, as well as support for STOMP.

        (4) Portlet module. It is similar to the function of the Servlet module and provides the MVC implementation in the Portlet environment.

d. Other modules

   Other modules of the Spring framework include AOP module, Aspects module, Instrumentation module and Test module.

Other modules Each module consists of

        (1) AOP module. It provides support for aspect-oriented programming. Programs can define method interceptors and entry points to separate code by function to reduce program coupling.

        (2) Aspects module. It provides support for integration with AspectJ.

        (3) Instrumentation module. It provides support for class tools and implements class loaders, which can be used in specific application servers.

        (4) Messaging module. It is a new module added after Spring 4.0, which provides support for messaging architecture and protocols.

        (5) Test module. It provides support for program unit testing and integration testing.

Fourth, the new features of Spring 5

Comparison of Spring 5 with other versions

        Spring 5 is the latest version of Spring. Compared with historical versions, Spring 5 has revised and updated the Spring core framework and added many new features, such as supporting responsive programming.

New features of Spring 5:

a. Update the JDK baseline

        Because the Spring 5 code base runs on JDK 8, the minimum requirement for JDK for Spring 5 is JDK 8, which can promote Spring users to actively use the new features of Java 8.

b. Revise the core framework

        (1) Based on the reflection enhancement of JDK 8, the methods provided by Spring 5 can be used to access classes or class parameters more efficiently.

        (2) The core Spring interface provides optional declarations built on JDK 8's default methods.

        (3) Use @Nullable and @NotNull annotations to indicate nullable parameters and return values, which can handle null values ​​at compile time instead of throwing NullPointerExceptions at runtime.

c. Update the core container

        Spring 5 supports candidate component indexing as an alternative to classpath scanning. Reading the entity class from the index will make loading the component index cheaper, so the startup time of the Spring program will be reduced.

d. Support responsive programming

 Reactive programming is another style of programming that focuses on building applications that respond to events. Spring 5 includes Reactive Stream and Reactor (Java implementation of ReactiveStream). Reactive Stream and Reactor support Spring's own functions and related APIs.

e. Support functional web framework

        Spring 5 provides a functional web framework. The framework uses a functional programming style to define endpoints, which introduces two basic components: HandlerFunction and RouterFunction. HandlerFunction represents processing the received request and generating a response function; RouterFunction replaces the @RequestMapping annotation and is used to forward the received request to the processing function.

f. Support Kotlin

        Spring 5 provides support for the Kotlin language. Kotlin is an object-oriented language that supports a functional programming style. It runs on the JVM, which can make the code more expressive, concise and readable. With the support for Kotlin, developers can do deep functional Spring programming, which broadens the application fields of Spring.

g. Improve the test function

        Spring 5 fully supports Junit 5 Jupiter, so you can use Junit 5 to write test code. In addition, Spring 5 also provides extensions for parallel testing in the Spring TestContext Framework. For the reactive programming model, Spring 5 introduces WebTestClient integration tests that support Spring webFlux.

A brief introduction to the Spring framework

        Spring is an independent framework, it does not need to depend on any web server or container, it can be used in both independent Java SE projects and Java EE projects. Before using Spring, you need to obtain its jar package, which can be downloaded from the Spring official website.

To download the related jar packages of Spring, follow the steps below.

        Use a browser to access the official download address of Spring, and visit the org→springframework→spring path, and you can see the download links of the compressed packages of each version of the Spring framework. Here, select version 5.2.8.RELEASE, and click the link to download the file.

Click the link below to download the file on the official website.

spring-5.2.8.RELEASE-dist.zip

After the download is complete, unzip the file to get a folder named spring-framework-5.2.8.RELEASE.

Folder introduction under the Spring directory structure

 docs folder:

        This folder stores Spring-related documents, including development guides and API reference documents.

 libs folder:

        The jar package and source code required for development are stored in this folder. The entire Spring framework consists of 21 modules. Spring provides three compressed packages for each module in the libs directory. Therefore, there are 63 jar packages in the libs folder. The 63 jar packages are divided into three categories.

 schema folder:

        This folder stores the XML Schema documents of Spring's various configuration files. ​​​​​​​​

Load the JAR package

  When developing with Spring, in addition to using the built-in jar package, Spring's core container also needs to rely on the commons.logging jar package. The jar package can be downloaded from the official address of commons. After the download is complete, you will get a compressed package named commons-logging-1.2-bin.zit. After decompressing the compressed package to the custom directory, you can find commons-logging-1.2.jar corresponding to the commons.logging jar package.

Guess you like

Origin blog.csdn.net/W_Fe5/article/details/131654613