70. Spring's IoC Inversion of Control

IOC (Inversion of Control) : Inversion of Control is a theory and a guiding ideology. Instruct developers how to use objects and manage objects. The creation of objects, assignment of properties, and the declaration cycle of objects are all managed by containers outside the code.

content

Understanding IOCs

Technical realization of IOC

Spring configuration file

Characteristics of objects created by the Spring container

Use IOC to simulate user registration operation

IOC Summary



Understanding IOCs

  • Control : object creation, property assignment, object declaration cycle management
  • Inversion : Shifts the developer's authority to manage the object to the container implementation outside of the code. Object management is done by the container.
  • Forward : The developer uses the new constructor to create objects in the code. Developers have mastered the creation of objects, the assignment of properties, and the entire process of object creation and destruction. The developer has full control over the object.

 Summary: Through the container, you can use the object in the container (the container has created the object, the object attribute is assigned, and the object is assembled).

             Spring is a container that can manage objects, create objects, and assign values ​​to properties

Technical realization of IOC

(Dependency Injection) DI (Dependency Injection) : The abbreviation is DI, which is a technical implementation of IoC. The program only needs to provide the name of the object to be used. How to create the object, how to find it from the container, and how to obtain it is implemented by the container itself.

  • Dependency:   For example, the ClassA class uses the properties or methods of ClassB, which is called ClassA depends on ClassB.

eg:

public class ClassB{
    
    public void createOrder(){}
}

public class ClassA{
    //属性
    private ClassB  b = new ClassB();
    
    public void buy(){
        b.createOrder();
    }
}

执行ClassA的buy()
ClassA a  = new ClassA();
a.buy();

        The Spring container is a super factory responsible for creating and managing all Java objects, which are called beans. The Spring container manages the dependencies between beans in the container, and Spring uses "dependency injection" to manage the dependencies between beans. Use IoC to achieve decoupling and decoupling between objects.

Spring configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans               http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

Spring standard configuration file:

  1. The root tag is beans
  2. Following the beans is the constraint file description
  3. Inside beans are bean declarations
  4. What is a bean: A bean is a java object, a java object managed by the spring container, called a bean

Characteristics of objects created by the Spring container

  • Container object (ApplicationContext) interface Obtain other java objects to be used through the ApplicationContext object, and execute getBean("id of <bean>")
  • By default, spring calls the parameterless constructor of the class to create an object
  • Spring reads the configuration file, creates all java objects at one time, and puts them in the map

Use IOC to simulate user registration operation

Requirement: Simulate user registration

analyze:

  • It is necessary to define a method insertUser(SysUser user) in the dao interface (UserDao) interface, define the implementation class MySqlUserDao of the interface, and the insertUser() method does not need to operate data, and the output "uses dao to perform the insert operation".

  • A service interface (UserService) needs to be defined, and the implementation class UserServiceImpl of the interface is defined. There is an attribute of type UserDao in the implementation class of service. There is a method addUser(SysUser user) in the service class.

  • The operation is addUser() { userDao.insertUser() } in the service class to complete the registration.

  • Define an entity class SysUser that represents the user's data.

        Spring manages multiple configuration files: the most commonly used configuration files are those that contain relationships. There is a general file in the project, which has an import tag that contains other configuration files.

grammar:

总的文件(xml)
<import resource="其他的文件的路径1"/>
<import resource="其他的文件的路径2"/>

关键字“classpath:”:表示类路径, 也就是类文件(class文件)所在的目录。 spring到类路径中加载文件
                   什么时候使用classpath: 在一个文件中要使用其他的文件, 需要使用classpath


IOC Summary

IOC: For   managing objects, placing objects in containers, creating, assigning, and managing dependencies.

        Decoupling is achieved by managing objects. IoC solves the coupling relationship between business logic objects, that is, the decoupling between service and dao.

What objects are spring suitable for managing as a container?

  • service object, dao object.
  • Tool class object.

Objects that are not suitable for handing to spring?

  • entity class.
  • Objects in the web such as servlet, listener, filter, etc. They are created and managed by tomcat.


Guess you like

Origin blog.csdn.net/m0_54925305/article/details/123135171