Spring and IoC

Inversion of Control (IOC, Inversion of Control) is a concept and an idea.

It refers to handing over the object invocation rights that are traditionally directly controlled by program code to the container, and the assembly and management of objects are realized through the container. Inversion of control is the transfer of control of an object from the program code itself to the outer container .

However, it should be noted that IoC also has limitations and cannot be used in distributed systems. That is, the external container to which it depends must be in the same JVM as the transferor of the control right.
IoC is a concept, an idea, which can be implemented in a variety of ways. There are two popular implementations: dependency injection and dependency lookup. Dependency injection is more widely used.
. Dependency lookup: Dependency Lookup, DL, the container provides the callback interface and context to the component, and the program code needs to provide a specific lookup method. Typically, lookups rely on the JNDI system.
. Dependency Injection: Dependency Injection, DI, program code does not do positioning query, these tasks are done by the container itself.
Dependency injection DI means that when the program is running, if it needs to call another object to assist, it does not need to create the callee in the code, but relies on the external container, which is created by the external container and passed to the program.
Spring's dependency injection has almost no requirements on the caller and the callee, and fully supports the management of dependencies between POJOs.
Dependency injection is currently the best way to decouple. Dependency injection allows Spring beans to be organized together in configuration files instead of hard-coded coupling.

First, the introduction of SpringDI

1. The underlying implementation principle of SpringDI

Spring DI = factory + reflection + configuration file
Abstract-oriented programming in traditional development, the caller and the interface implementation class are tightly coupled together. If the implementation class changes, the original code must be modified: the implementation class is replaced in the caller class. However, this is not in line with the OCP principle of software development (OCP, Open-Close Principle).
The OCP principle, the Open-Closed Principle, states that software entities (classes, modules, functions, etc.) should be extendable, but not modified. That is, it is open to function extension and closed to existing code modification. That is, new functionality should be extended without modifying existing code.
To decouple the caller class from the interface implementation class, use the factory pattern. However, this in turn couples the caller to the factory, which in turn couples the interface implementation class. The replacement of the implementation class does not need to modify the caller class, but the factory needs to be modified, that is, the code still needs to be recompiled.
At this point, the interface implementation class can be loaded by using "reflection + configuration file" in the factory class. And that's how Spring's IoC works.

2. Ordinary three-tier architecture programming

Example: common

Step1: Create a Java Project
Step2: Create Service layer interface IStudentService
Step3: Create interface implementation class StudentServiceImpl
Step4: Create Dao layer interface IStudentDao
Step5: Create interface implementation class StudentDaoImpl
Step6: Write test class MyTest

3. Factory mode programming

Example: factory
Step1: Copy the common project and change its name to factory
Step2: Create the factory classes ServiceFactory and DaoFactory
Step3: Modify the way to obtain the Service in MyTest

Step4: Modify the acquisition method of Dao in StdentServiceImpl

 

4. Factory mode + reflection programming

Example: reflect

Step1: Copy the factory project and change its name to reflect

Step2: Modify the Service factory class: use reflection to create a Service implementation class object

Step3: Modify the Dao factory class: use reflection to create a Dao implementation class object

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326106172&siteId=291194637