[SSM Framework] Introduction to Spring and IOC

Table of contents

1. Introduction to Spring

1. Spring Overview

2. Spring family  

3.Spring Framework

Spring Framework characteristics

Five functional modules of Spring Framework

2. IOC

1. IOC container

IOC thought

Implementation of IOC container in Spring


1. Introduction to Spring

1. Spring overview

Official website address: https://spring.io/

  • Spring is the most popular enterprise Java application development framework, used by millions of developers around the world to create performant, easily testable, reusable code.
  • The Spring Framework is an open source Java platform originally written by Rod Johnson and first released in June 2003 under the Apache 2.0 license.
  • Spring is a lightweight framework, with a base version only around 2 MB in size.
  • The core features of the Spring framework can be used to develop any Java application, but building web applications on the Java EE platform requires extensions. The goal of the Spring Framework is to make J2EE development easier, promoting good programming practices by enabling a POJO-based programming model.

2. Spring family 

Project list: https://spring.io/projects

3.Spring Framework

The Spring basic framework can be regarded as the Spring infrastructure, and basically any other Spring project is based on the Spring Framework.

Spring Framework characteristics

  • Non-intrusive: When developing an application using the Spring Framework, Spring has very little impact on the structure of the application itself. Zero pollution can be achieved to the domain model; only a few simple annotations are required to mark the functional components, which will not destroy the original structure at all, but can further simplify the component structure. This makes the structure clear, concise and elegant when developing applications based on Spring Framework.
  • Inversion of Control: IOC——Inversion of Control, reversing the direction of resource acquisition. Turn creating resources by yourself and requesting resources from the environment into the environment preparing resources, and we enjoy resource injection.
  • Aspect Oriented Programming: AOP——Aspect Oriented Programming, which enhances code functions without modifying source code.
  • Container: Spring IOC is a container because it contains and manages the lifecycle of component objects. Components enjoy containerized management, which shields programmers from a large number of details in the component creation process, greatly reduces the threshold for use, and greatly improves development efficiency.
  • Componentization: Spring realizes the combination of simple component configurations into a complex application. These objects can be combined in Spring using XML and Java annotations. This allows us to build super-large and complex application systems in an orderly manner based on components with clear functions and clear boundaries.
  • Declarative: Many functions that previously required writing codes can now be implemented by the framework only by declaring the requirements.
  • One-stop shop: On the basis of IOC and AOP, it can integrate open source frameworks and excellent third-party class libraries of various enterprise applications. Moreover, the projects under Spring have covered a wide range of fields, and many functional requirements can be realized using Spring on the basis of Spring Framework.

Five functional modules of Spring Framework

functional module Features
Core Container The core container, any function used in the Spring environment must be based on the IOC container.
AOP&Aspects Aspect Oriented Programming
Testing Provides integration with junit or TestNG testing frameworks.
Data Access/Integration Provides functions for data access/integration.
Spring MVC Provides integration capabilities for web applications.

2. IOC

1. IOC container

IOC thought

IOC: Inversion of Control, the translation is inversion of control.

①Traditional way of obtaining resources

Cooking by yourself: buying vegetables, washing vegetables, choosing vegetables, changing knives, cooking vegetables, participating in the whole process is time-consuming and labor-intensive. You must clearly understand all the details of the entire process of resource creation and master them proficiently.

When a component in an application needs to obtain resources, the traditional way is that the component actively obtains the required resources from the container. In this mode, developers often need to know how to obtain specific resources in specific containers, which increases learning costs while reducing development efficiency.

②Reverse control method to obtain resources

Order takeaway: place an order, wait, eat, save time and effort, and don't have to care about all the details of the resource creation process.

The idea of ​​inversion of control completely subverts the traditional way that application components obtain resources: the direction of resource acquisition is reversed—the container actively pushes resources to the required components, and developers do not need to know how the container creates resource objects Yes, you only need to provide a way to receive resources, which greatly reduces the cost of learning and improves the efficiency of development. This behavior is also known as the passive form of lookup.

DI

DI: Dependency Injection, translated as dependency injection .

DI is another way of expressing IOC: that is, components accept resource injection from containers in some predefined ways (for example: setter methods). Compared with IOC, this expression is more direct.

So the conclusion is: IOC is an idea of ​​inversion of control, and DI is a concrete realization of IOC.

Implementation of IOC container in Spring

Spring's IOC container is a landing product implementation of the IOC idea. Components managed in an IOC container are also called beans. Before creating a bean, you first need to create an IOC container. Spring provides two implementations of the IOC container:

①BeanFactory

This is the basic implementation of the IOC container, the interface used internally by Spring. For Spring itself, not for developers to use.

ApplicationContext

A subinterface of BeanFactory that provides more advanced features. For Spring users, used in almost all occasions

ApplicationContext instead of the underlying BeanFactory.

type name Introduction

ClassPathXmlApplicationContext

Create an IOC container by reading configuration files in XML format on the classpath

object

FileSystemXmlApplicationContext

Create IOC content by reading configuration files in XML format through file system paths

container object

ConfigurableApplicationContext

A subinterface of ApplicationContext that contains some extension methods

refresh() and close(), let ApplicationContext have startup,

The ability to close and refresh the context.

WebApplicationContext

Specifically prepared for web applications, create IOC container pairs based on the web environment

Object, and import and store the object in the ServletContext domain.

14-day learning training camp tutor course: Zheng Weizhong "Vue and SpringBoot create a holiday hotel management system"

Guess you like

Origin blog.csdn.net/Javascript_tsj/article/details/128012309