The paper come zhongjue shallow, and must know this to practice

The article Outlines

  • What is the spring
    1. Traditional Java Web application architecture
    2. Stronger Java Web application architecture — MVC framework
    3. Spring — Cohesive framework
  • The connotation of the spring
    1. Spring core
      • JavaBean
      • Application context
      • IoC/DI
    2. Spring Framework Components
    3. What else can Spring do

What is the Spring

The Spring framework is an open source full-stack application framework and inversion of control container implementation for the Java platform.

Traditional Java Web application architecture

Traditional Java Web frameworks typically have two layers: Contrler-Model.

The controller receives and processes requests through servlets and generates dynamic content according to different Web requests. The model layer mainly deals with the interaction with the database, receives requests from the controller, and packages the obtained data back to the controller.

The main process is as follows:

  1. The Web browser sends HTTP requests to the server, which are picked up and processed by controllers (mostly servlets)
  2. The Controller invokes the core business logic — Model
  3. The Model performs database access operations and returns the results to the Controller
  4. The Controller passes the results of the business logic processing to the View (usually a JSP) and dynamically outputs the HTML content
  5. Dynamically generated HTML content is returned to the browser for display

Stronger Java Web Architecture — MVC Framework (Spring MVC Framework as an example)

Spring MVC is implemented based on the Model-View-Controller (MVC) pattern, which can build a flexible and loosely coupled Web application. The request flow based on Spring MVC architecture is as follows:

  1. The client makes a request and submits the data to the server.
  2. Distributed through the front controller, requests are delegated to other components of the application (controllers) to execute the actual requests by querying processor maps.
  3. The controller performs the logical processing, generates the response information (model), packages the model data, identifies the logical view name (used to determine which view to render output with), and returns the model and view name to the front end controller.
  4. The front controller uses the view resolver to match the logical view name to a specific view implementation.
  5. The view uses model data to render output, which is passed to the client through the response object.

Spring — Cohesive framework

One-stop bonding frame, it is like magic glue, can make each part of the organic bonding together in a loose coupling way.

Spring MVC invokes a large number of components to complete a user request and return a response, and each component has one or more Javabeans that coordinate with each other. In order to reduce coupling and make Bean creation and connection between beans more convenient and flexible, Spring adopts the idea of Inverse of Controller (IoC), which moves the strongly coupled code dependencies out of the code and into a unified XML configuration file. It is called Dependency Injection (DI), in which the main control of the program for components is handed over to IOC container, which uniformly loads and manages them, and the creation, association and properties of beans are assigned to beans. Therefore, Dependency Injection is closely related to inversion of control.

Thus, the core technical idea of the Spring framework is inversion of control/dependency injection. The core content is: beans.

The connotation of the Spring

Spring core

JavaBean

Spring’s official documentation explains beans as follows:

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

Translation:

The objects that make up the backbone of an application and are managed by the Spring IoC container are called Beans. Beans are objects instantiated, assembled, and managed by the Spring IoC container.

Summary:

  1. The Bean is an object
  2. Beans are the backbone of a Spring application
  3. Beans are centrally managed by the Spring IoC container

Spring Application Context

We already know that the Spring IoC container has control over object creation, management, etc., but we need to put objects into the container and determine the dependencies between objects. In general, the container just provides a space to manage objects. We need to add management-managed objects to the container through Spring’s application context.

A Spring application context can be simply understood as a container object that puts spring-managed objects into a container. In other words, the application context is an abstract representation of the Spring container. The ApplicationContext used by the above code is essentially a high-level interface that maintains Bean definitions and collaboration between objects.

At the heart of Spring is the container, which uses DI to manage the components that make up your application. Spring comes with multiple container implementations that fall into two different types. The BeanFactory is the simplest container that provides basic DI support. The application context is built on BeanFactory, which provides more services, such as parsing configuration text information, and so on.

IoC/DI

Beans in the Spring framework are not traditionally created in the new way. Instead, they are managed through a unified container called Spring IoC that has the management rights to instantiate, configure, and assemble beans. The Spring IoC container reads configuration metadata to get information about objects that need to be instantiated, configured, and assembled. Configuration metadata information is typically represented as XML files or Java annotations or Java code.

Code examples:

Interface:

public interface HelloService {
    public void sayHello(a);
}
Copy the code

Implementation class:

public class HelloServiceImp implements HelloService {
    public void sayHello(a) {
        System.out.println("Hello World!"); }}Copy the code

As mentioned earlier, metadata information can be configured in three ways, in this case XML:

helloworld.xml

<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <! -- id indicates the component name, class indicates the component class -->
    <bean id="helloService" class="com.HelloServiceImpl" />
    
</beans>
Copy the code

Instantiate an IoC container:

public class HelloServiceTest {
    public void testHelloWorld(a) {
        // Load the Spring context, read the configuration file to instantiate an IOC container
        ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");
        // get the Bean from the container
        HelloService helloService = context.getBean("helloService", HelloService.class);
        // 3. Execute the service logichelloService.sayHello(); }}Copy the code

As you can see, the top-level interface specifies the methods, and underneath it you can have various implementation classes that print out different things. The implementation class is registered as a bean with a specific ID by configuring XML. In the instantiation container section, metadata information is first read through the application context to get an instance of the container, then a bean is obtained and injected into the Service, which then executes the specific method. As you can see, our Service does not need to know what it is printing out, just to do what it is supposed to do, each part doing its own thing, not interfering with each other but cooperating with each other.

Spring Framework Components

Spring Core Container: The core container provides the basic functionality of the Spring framework. The main component of the core container is the BeanFactory, which is an implementation of the factory pattern. The BeanFactory uses an inversion of Control (IOC) pattern to separate the application’s configuration and dependency specifications from the actual application code.

Aspect Oriented Programming: This module is the basis for developing aspects in Spring applications. AOP helps to decouple application objects — to decouple concerns (such as transactions, security, and so on) from the objects to which they apply.

Data access and integration: This module abstracts a lot of boilerplate code, making database code straightforward. The Spring ORM module is built on top of DAO support and provides an easy way to build DAOs for multiple ORM frameworks such as Hibernate, iBATIS SQL Maps, etc.

Web and Remote Invocation: Spring MVC comes with its own MVC framework. Integrated WITH Remote Method Invocation (RMI), Hessian, Burlap, JAX-WS, and HTTP Invoker.

Testing: Spring provides a list of mock object implementations for writing endpoint tests using JNDI, servlets, and portlets.

What else can Spring do

The Spring Portfolio includes multiple frameworks and class libraries built on top of the core Spring framework. The entire Spring Portfolio provides a Spring programming model for virtually every area of Java development.

Spring Boot: Focuses on simplifying Spring itself from a Spring perspective. Spring Boot relies heavily on auto-configuration technology, which eliminates most Spring configuration.

Spring Web Flow: Built on the Spring MVC framework, it provides support for process-based, conversational Web applications such as shopping carts, wizards, and so on.

Spring Web Service: Provides a contract-first Web Service model, where implementations of services are written to meet Service contracts.

Spring Security: Using Spring AOP, Spring Security provides declarative Security mechanisms for Spring applications.

Spring Integration: To facilitate direct application interaction, Spring Integerration provides Spring declarative implementations of various common application Integration modes.

Spring Batch: Suitable for developing Batch applications, such as those that require a large number of operations on data.

Spring Data: Whether you’re using a document database (e.g. MongoDB), a graph database (e.g. Neo4j), or a traditional relational database, Spring Data provides a simple programming model.

Spring Social: Spring Social is more about connect than Social, and it helps you connect to Spring applications through REST apis.

Spring Mobile: A new extension to Spring MVC to support Mobile Web application development.

If there are mistakes, please forgive and put forward, progress together 😀

References:

Spring In Action (4th Edition)

How do Java novices learn Spring, Struts and Hibernate

Introduction to Spring Framework

Introduction and use of the Spring framework

What is Spring MVC