Understanding of IOC and AOP in spring

IOC : Inversion of Control is also called Dependency Injection . The factory pattern is used .

 In order to facilitate understanding, it is broken down into each item for easy memory.
  1. To hand over the object to the container for management, you only need to configure the corresponding bean in the spring configuration file, and set the relevant properties, so that the spring container can generate the instance object of the class and the management object. 2. When the spring container starts, spring will initialize all the beans you configured in the configuration file. 3. When you need to call, assign those beans that have already been initialized to the ones you need to call these beans. Class (assuming that the class name is A), the allocation method is to call the setter method of A to inject, without requiring you to new these beans in A.
Note: During the interview, if you have the conditions, draw a picture, which will make it more obvious that you understand.

Spring's IOC container can help us automatically new objects. After the objects are handed over to spring, we don't need to manually go to new objects, that is, the transfer of control.
Spring uses BeanFactory to instantiate, configure and manage objects, but it is just an interface with a getBean() method in it.
We generally do not use BeanFactory directly, but use its implementation class ApplicationContext This class will automatically parse our configured applicationContext.xml


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 an object hierarchy that simulates a collection of common behaviors. When we need to introduce common behavior for distributed objects, OOP is powerless. That is, OOP allows you to define top-to-bottom relationships, but is not suitable for defining left-to-right relationships. For example the log function. Logging code tends to be spread horizontally across all object hierarchies, irrespective of the core functionality of the objects to which it spreads. In OOP design, it leads to a lot of code duplication, which is not conducive to the reuse of individual modules.
  AOP encapsulates the cross-business logic (such as security, logging, transaction, etc.) in the program into an aspect, and then injects 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 the method of intercepting the message to decorate the message to replace the execution of the original object behavior; the other is to use the static implantation method (static proxies), introduce a specific syntax to create "aspects", so that the compiler can weave code about "aspects" during compilation.
Simple explanation, let's say you want to add a print' to all classes in your biz layer You can use the aop idea to do the function of hello. You first write a class and write a method. After the method is implemented, print 'hello' and let you Ioc this class ref=" biz .*" to make each class injection.

Reprinted: https://www.cnblogs.com/ScarecrowAnBird/p/6737319.html

Guess you like

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