In-depth analysis of the basic concepts and core ideas of Spring

basic concept

  1. After using spring, classes managed by spring do not need to be new.
  2. If you want a class to be managed by spring, you need to introduce the class into the spirng configuration file through a bean.

IoC

IoC container

The simple understanding is: the software that implements IoC ideas and provides object creation, object assembly and object life cycle management is the Ioc container

IoC understanding

  1. The application does not need to actively new objects, but describes how the objects are created.
  2. The application does not need to actively assemble the dependencies between objects, but describes which services are needed. The IoC container will assemble it for you and passively accept the assembly.
  3. Active to passive is a component design method that prevents service consumers from directly relying on service providers. It is a design principle that reduces dependencies between classes.

IoC steps

Spring mainly implements IOC through bean tags.

<!-- id属性给这个bean指定一个唯一的名,供spring的其他组件来引用 
     class属性指定bean的全路径
-->
<bean id="hello" class="com.hello.Hello">
   <!--property通过set方法给对应的属性设置值
       value属性可以直接设置值
       ref属性引用其他的组件
   -->
   <property name="who" value="t6041"></property>
   <property name="ink" ref="inkColor"></property>
</bean>

Steps to use ioc in Spring

  1. Create class
  2. Put beans into spring through bean tags
  3. Initialize the spring configuration file to get the spring context, and obtain the corresponding beans through the spring context.

Aop

Aop understanding

Decompose complex requirements into different aspects, assemble and run the "public functions" scattered in the system
using a proxy mechanism for "centralized solution", enhance the code segments on the basis of "not changing the source program", and add new Function

Aop steps

  1. Create an enhancement. And incorporated into the spring container. Make the normal class into enhanced.
    • Use tags like aop:before to turn normal into enhanced
    • Implementing the BeforeAdvice interface can also turn ordinary classes into enhanced
<bean id="loggerAdvice" class="com.advice.LoggerAdvice"></bean>
  1. Create entry points.
<aop:pointcut expression="execution(public void print())" id="pointcut"/>
  1. Define aspects. Include methods and entry points for enhancement.
<aop:aspect ref="loggerAdvice">
    <aop:before method="before" pointcut-ref="pointcut"/>
</aop:aspect>

Inversion of control

Who controls whom? What is controlled? Why is it called reversal (corresponding to forward direction)? Which aspects are reversed? Why is reversal needed?

  1. Who controls whom? -->loC/DI container controls application
  2. What to control? -->IoC/DI container controls the creation and instantiation of the object itself; IoC/DI container controls the dependencies between objects
  3. Why is it called reverse (corresponding to forward)? -->Because now the application can no longer actively obtain external resources, but passively waits for the IoC/DI container to inject it with the resources it needs, so it is called reverse
  4. What aspects are reversed? -->1. Creating objects 2. The way the program obtains resources is reversed
  5. Why is reversal needed? -->1 After the introduction of the IoC/DI container, the system is looser and the management is more orderly; 2. Loose coupling is truly realized between classes

rely

What is dependence (understood by name, understood by verb)? Who depends on whom? Why do we need to depend? What do we depend on?

  1. What is dependence (understood by name, understood by verb)? -->Dependence (understood by name): dependence relationship; dependence (understood by verb) dependent action
  2. Who depends on whom? -->The application depends on the IoC/DI container
  3. Why do we need dependencies? -->Because of the inversion, the resources that the application depends on are all in the IoC/DI container.
  4. What does it depend on? -->The application depends on the IoC/DI container, which injects the required resources into it

injection

Who injects into whom? What is injected? Why is it injected?

  1. Who injects into whom? -->IoC/DI container injects into the application
  2. What to inject? -->Inject external resources needed by the application, such as relationship dependencies
  3. Why inject? -->Because the program needs these external resources to run normally

Are dependency injection and inversion of control the same concept?

They are not the same concept. In fact, they both describe the same event, but from different perspectives: Inversion of control is from the perspective of the IoC/DI container; dependency injection is from the perspective of the application.

  1. Description of Inversion of Control: The IoC/DI container in turn controls the application and controls the external resources required by the application (for example: external resources)
  2. Dependency injection description: The application depends on the IoC/DI container, which injects the external resources it needs.

What is IoC/DI

What is IoC/DI? What can it do? How to do it? Where is it used?

  1. What is IoC/DI
    • IoC: It is a program development idea that uses the IoC/DI container to control the external resources required by the application.
    • DI: It is a program development idea that the application relies on the IoC/DI container to inject the required external resources.
  2. What can be done --> Loosely coupled objects
  3. How to do it-->Use Spring framework, which has implemented IoC/DI container
  4. Where to use it --> Whenever external resources are needed in the program, you can consider using IoC/DI containers

What are external resources

For a class, the so-called external resources refer to things that cannot be obtained or implemented in its own class. For example: if you want to read a configuration file in a class, then this configuration file is equivalent to the external resource of this class, and For example: if class A calls class B, then for class A, class B is an external resource.

data access

Spring provides support for various data access technologies, including JDBC, ORM (object-relational mapping) frameworks (such as Hibernate, MyBatis), transaction management, etc. It provides a unified way to access data, and can easily switch or combine different data access technologies.

  1. JDBC: Spring provides the JdbcTemplate class to simplify JDBC operations, including connection management, exception handling and transaction management, etc.
  2. ORM (Object Relational Mapping): Spring provides integrated support for ORM frameworks, including Hibernate, JPA, MyBatis, etc. Through Spring's ORM support, database operations can be performed more conveniently without writing cumbersome database access code.
  3. Transaction management: Spring provides encapsulation and management of transactions, including programmatic transactions and declarative transactions. By using Spring's transaction management support, you can simplify the use of transactions and improve the maintainability and readability of your code.
  4. Database connection pool: Spring provides integrated support for common database connection pools, such as C3P0, HikariCP, etc. By using Spring's database connection pool, you can manage connection resources more efficiently and improve application performance and scalability.
  5. NoSQL database: Spring provides integrated support for some NoSQL databases, such as MongoDB, Redis, etc. Through the support of Spring, these NoSQL databases can be more conveniently operated and data persistence and caching can be achieved.

Wed development

Web is a module in the Spring framework used to develop web applications. It provides a powerful set of classes and tools for simplifying the development and management of web applications.

  1. MVC (Model-View-Controller) architecture: Spring Web provides a Web framework based on the MVC architecture. By defining and organizing controllers, views, and models, developers can more easily separate business logic and interface interaction.
  2. Request processing: Spring Web provides a variety of ways to handle HTTP requests, including annotation-based request mapping, path variables, request parameter binding, request verification and error handling, etc. Developers can choose the most appropriate request handling method based on specific needs.
  3. View rendering: Spring Web supports a variety of view technologies, including JSP, Thymeleaf, Freemarker, etc. Developers can choose the appropriate view rendering method according to specific needs.
  4. Form processing: Spring Web provides support for form processing, including form data binding, validation, error handling, and defense against repeated form submissions.

Guess you like

Origin blog.csdn.net/m0_65491952/article/details/132332783