Spring IOC/DI and AOP principles

IOC/DI

  1. Conceptual machine principle

  IOC: Inversion of Control (Inversion of Control) is a design idea, that is, the container controls the creation and management of external resources required by the application, and then inverts it to the application. The creation and maintenance of objects and their dependent objects do not need to be implemented in the application, and they are handed over to the IOC container for management . In traditional development, we create the dependent object inside the object and inject the current object to complete the maintenance of the dependency relationship; for IOC, it emphasizes that the active becomes passive , and the IOC container is responsible for the creation and search of the dependent object . The IOC container injects the combined objects, and we only need to maintain the dependencies between the objects in the relevant configuration files.

  DI: Dependency Injection, that is, "dependency injection". In fact, IOC and DI are two different expressions of the same concept. The application relies on external objects provided by the container, and the container injects the external resources it depends on into the application at runtime; when an object is called, its Dependent objects have container injection.

 

  2. Advantages

  First, the centralized management of resources realizes the configurability and easy management of resources, and reduces the complexity of object relationship maintenance.

  Second, it reduces the degree of dependence on both parties using resources, which is what we call coupling.

 

      3. Three ways of dependency injection:

        (1) Interface Injection : When the container is running, the target object implements a specific interface, and then the container injects the associated object into the target object through the interface. This way Spring framework does not support, so understand

        (2) Constructor Injection : When the container is running, the associated object is injected into the object through the class constructor. The Spring framework also supports it, and it is not as common as set .

        (3) Setter Injection ( set method injection ) : When the container is running, the associated object is injected into the target object through the set method of the class attribute. Spring framework support, and it is often used in our development, it is very important.

 

      4. Inversion of Control (IoC) and Dependency Injection (DI) are the same concept, the purpose of introducing IOC:

          (1) Detach and reduce the coupling between classes; (2) Advocate interface-oriented programming and implement the principle of dependency switching; (3) Improve the system's pluggable, testable, and modifiable features.


      5. Specific practices:

         (1) Change the dependencies between beans to associations as much as possible;
         (2) Convert the associations of concrete classes to associations of Java interfaces as much as possible, instead of being associated with specific service objects;
         ( 3) The instance of which implementation class of the relevant Java interface is specifically associated with the Bean instance is described in the metadata of the configuration information;
         (4) The IoC component (or container) instantiates the specific bean class according to the configuration information, and connects the beans between the beans. The dependencies are injected in.

 

 =====================================The following is AOP============ =============================

 

Two AOPs

  1. Concepts and Principles

  AOP uses a technique called "cross-cutting" to dissect the inside of the encapsulated object, and encapsulate the common behavior that affects multiple classes into a reusable module, which is called Aspect. The so-called "aspect", simply put, is the part that encapsulates the logic that has nothing to do with the business but is commonly invoked by the business modules. In order to reduce the repetitive code of the system, reduce the coupling degree between modules, and facilitate system maintenance.

  Using "cross-cutting" techniques, AOP divides the software system into two parts: core concerns (business logic) and cross-cutting concerns (common logic, or aspects). The main flow of business processing is the core concern, and the less relevant part is the cross-cutting concern. The characteristic of cross-cutting concerns is that they often occur in multiple places in the core concern, but are basically similar everywhere. Such as authorization authentication, logging, transaction processing, debug management, performance testing, etc. The role of AOP is to separate various concerns in the system, separating core concerns and cross-cutting concerns.

  2. Implementation

    The technologies for implementing AOP are mainly divided into two categories:

  One is to use dynamic proxy technology to decorate the message by intercepting the message to replace the execution of the original object behavior;

  The second is to use static weaving to introduce a specific syntax to create "aspects", so that the compiler can weave the code related to "aspects" during compilation.

  3. Advantages

  ①. The codes of cross-cutting concerns are concentrated in one piece, and are no longer scattered in various business components, and there will be no large amount of repetitive code;

  ②. The core module only focuses on the code of the core function, which is separated from the general module, and the coupling degree between the modules is reduced.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326576067&siteId=291194637