Get to know SpringIOC container

Table of contents

1. SpringFrameWork

2. SpringIOC container

3. Core functions of SpringIoc


1. SpringFrameWork

1.The relationship between SpringFrameWork and Spring

SpringFrameWork is the basic implementation of SpringCould, SpringMVC and other technologies, and all SpringCloud, SpringFrameWork, SpringMVC and other technologies are combined to form the Spring family.

 Broadly speaking:

  1. Spring in a broad sense refers to the Spring technology stack based on the Spring Framework.
  2. After more than ten years of development, Spring is no longer a simple application framework, but has gradually developed into a mature technology composed of multiple different sub-project modules), such as Spring Framework, Spring MVC SpringBoot, Spring Cloud, Spring Data Spring Security, etc., Spring Framework is the basis for other sub-projects.
  3. These sub-projects cover everything from enterprise-level application development to cloud computing. They can help developers solve various practical problems that constantly arise during the software development process and bring developers a better development experience.

narrow sense:

  1. Spring in the narrow sense refers specifically to Spring Framework, which we usually call Spring Framework.
  2. Spring Framework (Spring Framework) is an open source application framework developed by SpringSource Company. It was originally created to solve various common problems in enterprise development. It provides many functions, such as: dependency injection (Dependency injection), aspect-oriented programming (AOP), declarative transaction management (TX), etc. Its main goal is to make the development of enterprise-level applications easier and faster, and the Spring framework is widely used in the field of Java enterprise development.
  3. Other frameworks of Spring Family Bucket are based on the Spring Framework framework!

1.2 As a basic framework, you need to understand what a framework is

Framework = combination of jar package and configuration file.

For example, the difference between log4j and dbutil.

dbutil (database simplification technology): It is just a simple jar package, only using its related methods, so it is just a tool class log4j (log output technology): Although there are only two methods in its jar package, it can configure related log4j .propertis to modify the content of the log output, so this is a framework.

1.3 Functions of SpringFrameWork

SpringFrameWork has four most important functions, namely Core Container, AOP&Aspect, TX, and SpringMVC.

functional module Features
Core Container Any function used in the Spring environment must be based on the IOC container
AOP & Aspects Aspect Oriented Programming
TX Declarative transaction management
Spring MVC Integrated features for web applications

1.4 Advantages of SpringFrameWork

  1. Rich ecosystem: Spring Framework is supported by a large open source community and provides a variety of extensions and plug-ins, such as Spring Security, Spring Data, etc., enriching developers' choices and scalability.
  2. Modular design: Spring Framework adopts modular design, splitting functions into independent modules. Developers can choose to use different modules according to their needs, which improves the flexibility and maintainability of the project.
  3. Simplify Java development: Spring Framework provides features such as dependency injection (DI) and aspect-oriented programming (AOP), which simplifies the development process of Java applications, reduces the writing of boilerplate code, and improves development efficiency.
  4. Continuous innovation and development: Spring Framework continues to carry out technological innovation and development, introducing new features and functions, such as reactive programming, functional programming, etc., to continuously meet the development needs in different scenarios and keep pace with the times.

2. SpringIOC container

2.1 The difference between SpringIOC container and ordinary container

First, understand what ordinary containers and complex containers are

  • Ordinary containers: Just like collections in the Java class collection framework, they can only store and retrieve functions.
  • Complex container: can manage some relatively complex transactions such as the relationship between contents, such as a servlet container, which can receive request information and operate on the request information.

SpringIoc can manage the container of components, which is a complex container that can not only store components, but also manage dependencies between components, create and destroy components, etc.

2.2 What are components

Components are objects that can be reused

For example, in a three-tier framework, the three-tier architecture can be split into three components

2.3 What are the advantages of Spring management components?

  1. Reduce the coupling between components: Spring manages components through dependency injection (DI), so that the dependencies between components are dynamically managed by the container, reducing the tight coupling between components and improving the scalability and flexibility of the code. sex.
  2. Improved code reusability and maintainability: By handing over the creation and configuration of components to Spring container management, components can be flexibly replaced, upgraded and configured without modifying the original code, improving the reusability of components. performance and maintainability.
  3. Convenient configuration and management: Spring provides a variety of configuration methods, such as XML configuration, annotation configuration and Java configuration, which can flexibly configure and manage components. At the same time, the Spring container uniformly manages the life cycle of components, simplifying component configuration and management. management process.
  4. Objects (components) managed by Spring can enjoy other functions of the Spring framework such as AOP, transaction declaration, etc.

2.4 Specific interfaces and implementation classes implemented by SpringIoc

1. Two specific interfaces

interface Features
BeanFactory Provides the basic functions of the IOC container for managing and obtaining Bean objects
ApplicationContext Provides more advanced functions based on BeanFactory, such as event processing, internationalization support, etc.

 The relationship between the two interfaces:

  1. BeanFactory is the most basic container of Spring, providing the basic functions of IoC container for managing and obtaining Bean objects. It is an implementation of the factory pattern, responsible for creating and managing the life cycle and dependencies of Beans. BeanFactory has the feature of lazy loading and will only be instantiated when the beans need to be obtained.
  2. ApplicationContext is extended on the basis of BeanFactory and provides more advanced functions and features. ApplicationContext will pre-instantiate and initialize all Bean objects at startup and cache them in the container, thus providing faster access. In addition to the functions of BeanFactory, ApplicationContext also supports event processing, internationalization support, resource loading and other functions.

In short, BeanFactory provides the configuration framework and basic functionality, while ApplicationContext adds more enterprise-specific functionality. ApplicationContext is a complete superset of BeanFactory! 

2.5Four implementation classes of the ApplicationContext interface

Type name describe
ClassPathXmlApplicationContext Create an IOC container object by reading the XML format configuration file under the classpath. It is suitable for placing the configuration file under the class path of the project, such as the configuration file in the src/main/resources directory.
FileSystemXmlApplicationContext Read the configuration file in XML format through the file system path to create an IOC container object. Applies to configuration files specifying absolute or relative paths.
AnnotationConfigApplicationContext Create an IOC container object by reading the Java configuration class. It is suitable for annotation-based configuration and does not require the use of XML files for configuration.
WebApplicationContext Specially prepared for Web applications, create IOC container objects based on the Web environment, and import and store the objects into the ServletContext domain. Suitable for Web projects to obtain Web-related contextual information.

A Spring framework only needs to choose a subclass implementation method

The following are the conditions for the implementation of the four implementation classes

  • ClassPathApplicationContext: 1. Use XML configuration 2. Resource under the project’s class path
  • FileSystemApplicationContext: 1. Use XML configuration 2. Store the file outside the project, under a certain drive letter
  • AnnotationConfigApplicationContext: 1. The configuration file uses Java classes
  • WebApplicationContext: 1. The web project corresponds to the ioc container

2.6 Spring framework configuration method

  1. XML configuration method:

    • Description: One of Spring's earliest configuration methods defines beans and their dependencies, scope and other information through XML files, and the Spring IOC container manages the dependencies between beans.
    • Features: Supported from the first version of the Spring framework.
  2. Annotation method:

    • Description: Supported starting from Spring 2.5, using annotations to replace configuration information in XML configuration files. By using annotations (such as @Component, @Service, @Autowired, etc.) on the Bean class, the Bean is registered in the Spring IOC container to achieve dependency management.
    • Features: Simplify configuration and improve development efficiency.
  3. Java configuration class method:

    • Description: Supported starting from Spring version 3.0, using Java classes to define Bean objects, dependencies and configuration information, replacing XML configuration files. Implement the configuration of beans and dependencies through @Configuration, @Bean and other annotations.
    • Features: Use Java code to write configuration, high type safety, and can use object-oriented features for configuration.

The current mainstream configuration: configuration class + annotation method (catering to SpringBoot) mainly for annotation


3. Core functions of SpringIoc

There are two core functions of Spring: SpringIoc (Inversion of Control) and DI (Dependency Injection)

What is inversion of control Ioc (Inversion of control)

        It refers to a design principle that transfers control of object creation and invocation from the application to the container. In IoC, applications no longer directly create and manage objects. Instead, the IoC container is responsible for creating and managing objects, and injects object dependencies into the application through dependency injection.

The main features are as follows:

  • Control transfer: The application no longer directly controls the creation and invocation of objects, but delegates management to the IoC container.
  • Dependency injection: Through dependency injection, object dependencies are injected into the application, which solves the coupling between objects and improves the flexibility and testability of the code.
  • Dependency lookup: The application uses the IoC container to find the objects it needs to use instead of creating the objects itself, reducing code dependencies.

What is dependency injection DI (Dependency inject)

Create two objects A and B. The B object passes object A into object B through setter and other methods.

Dependency Injection (DI) refers to the process of transferring dependencies between components by leaving the processing of dependencies to the container, thereby avoiding hard-coding dependencies between objects in the application code and implementing objects decoupling between. In the Spring framework, DI can be implemented through XML configuration files or annotations, and provides three forms of dependency injection:

  1. Constructor Injection: By annotating the constructor of the class or configuring the corresponding dependencies in the XML configuration file, the container will automatically pass the required dependencies as parameters to the constructor when creating the object. Complete Object creation and dependency injection.

  2. Setter method injection (Setter Injection): By annotating the Setter method of the class or configuring the corresponding dependencies in the XML configuration file, the container calls the corresponding Setter method after creating the object and passes in the required dependency objects to complete the object. Dependency injection.

  3. Interface Injection: By defining member variables of the interface type in the class, implementing the corresponding interface in the class, and configuring the corresponding dependencies in the XML configuration file, after the container creates the object, it will call the interface for the dependent object. method to inject into the class.


Summarize:

1. Know the relationship between SpringFrameWork and Spring

2. Master the core functions of SpringFrameWork

3. Master the two interfaces and four implementations of SpringIoc

4. Master the three methods of Spring framework configuration

5. Understand Ioc control inversion and DI dependency injection

application:

Implementing SpringIoC configuration based on XML_Alphamilk's blog-CSDN blog 

Guess you like

Origin blog.csdn.net/dogxixi/article/details/132457110