1. Spring's IOC and AOP mechanism?

We are in the process of using the Spring framework, in fact, to use IOC, dependency injection, and AOP, aspect-oriented programming , these two are the soul of Spring.
The main design patterns used are factory pattern and agency pattern .
IOC is a typical factory model, injecting instances through sessionfactory.
AOP is the embodiment of a typical proxy model.
The proxy mode is a commonly used java design mode. Its characteristic is that the proxy class has the same interface as the delegate class. The proxy class is mainly responsible for
preprocessing messages, filtering messages, forwarding messages to the delegate class, and processing messages afterwards. Usually there is any relationship between the agent class and delegate class, the object of a proxy class is associated with a delegate class of object, the object proxy class itself is not real service, but through
any relevant method calls the delegate class of objects, To provide specific services.
Spring's IoC container is the core of spring, and spring AOP is an important part of spring framework.
In traditional programming, when the caller needs the assistance of the callee, the caller usually creates an instance of the callee. But the work was created in the spring where the caller is no longer done by the caller, so Inversion of Control (IoC); create Callee Instances work through
often done by the spring container, and then injected into the caller, it is also known For dependency injection (DI), dependency injection and inversion of control are the same
concept.
Aspect-Oriented Programming (AOP) considers program structure from another angle, and perfects object-oriented programming
(OOP) by analyzing the focus of program structure . OOP decomposes the application into objects at various levels, while AOP decomposes the program into multiple aspects. spring AOP only
Now that the method-level connection points are present, in J2EE applications, it is sufficient for AOP to intercept method-level operations. In spring, to make
IoC convenient to use robust and flexible enterprise services in the future , it is necessary to use spring AOP to establish a connection between IoC and enterprise services.
IOC: Inversion of control is also called dependency injection. Using the factory model
to hand over objects to the container for management, you only need to configure the corresponding beans in the spring configuration file and set the relevant attributes, so that the
spring container can generate class instance objects and management objects. When the spring container starts, spring will
initialize all the beans you configured in the configuration file , and then when you need to call, it will assign the initialized
beans to the classes you need to call these beans (assuming The class name is A), the method of allocation is to call A's setter method to inject, without requiring you to
add these beans in A.
Note: During the interview, if you have the conditions, draw a picture, so that you understand it more.
Spring ioc initialization process
AOP: aspect-oriented programming. (Aspect-Oriented Programming)
AOP can be said to complement and improve OOP. OOP introduces concepts such as encapsulation, inheritance, and polymorphism to establish a hierarchy of objects
to simulate a collection of public behaviors. When we need to introduce public behaviors for scattered objects, OOP is powerless.
In other words, OOP allows you to define a relationship from top to bottom, but it is not suitable for defining a relationship from left to right. For example, log function. Log
code is often distributed horizontally in all object levels, and has nothing to do with the core functions of the objects it distributes. In OOP design, it
leads to a lot of code duplication, which is not conducive to the reuse of various modules.
Encapsulate the cross-business logic in the program (such as security, log, transaction, etc.) into an aspect, and then inject it into the target object (specific business logic).
The technologies for implementing AOP are mainly divided into two categories: one is the use of dynamic proxy technology , which uses intercepting messages to
decorate the message to replace the execution of the original object behavior; the other is the use of static weaving . Introduce a specific grammar to create "aspects", so that
the compiler can weave code related to "aspects" during compilation.
Simply explain, for example, you want to add a print "you" to all classes in your biz layer. "Good" function, then you can use the aop idea to do it.
You first write a class and write a class method, the method is implemented to print "Hello", and then the Ioc class ref = "biz.*" so that each class is It can be achieved by injection.

Guess you like

Origin blog.csdn.net/m0_51684972/article/details/109215511