IOC and AOP are two core features of Spring, and they are often tested in interviews.

Spring IOC

IOC stands for inversion of control, which, as the name implies, reverses the control process in Spring, handing control of objects to an external container. In short, the creation, initialization, and destruction of objects are left to Spring rather than managed by the developer.

The original object creation and use required the developer to actively create (new) objects and then perform various other operations on them

However, if you use IOC, the IOC container layer implements the object factory, which does the following:

2) Configure the newly created object using an XML configuration file and modify the bean path of the XML. 3) Parse the path using the XML file. 4) Get the contents of the class by reflecting the

IOC implements two interfaces: the underlying object factory BeanFactory interface and the ApplicationContext interface.

The BeanFactory interface instantiates the injected XML. Calling it creates an object. ApplicationContext is a subinterface of the BeanFactory that creates an object as soon as the configuration file is read. A class from the root of ClasspathXmlApplicationContext loading configuration files, another is from disk loading configuration file path FileSystemXmlApplicationContext factorybean is bean generation tool, The BeanFactory is instantiated

Without mentioning IOC, DI(dependency injection), inversion of control is the reversal of how dependent objects are acquired. Spring provides dependency injection in four ways:

Constructor injection 2 setter method injection 3 Static factory injection 4 instance factory injection

Spring AOP

Spring AOP is a faceted technology. In the system, auxiliary functions such as permission function and log function are usually added throughout each part of the system. If you add them in a common way, the complexity of the system will be greatly increased. However, these functions are irrelevant to services.

AOP takes apart the inside of the enveloped object and wraps the common modules that affect multiple classes into a reusable module, naming it with annotations as aspects, or facets. The so-called aspect refers to those unrelated to the business, but the business code calls the logic together, these are encapsulated to reduce the duplication of code in the system, reduce the coupling degree between modules.

Using crosscutting techniques, AOP divides the system into two parts, core concerns and crosscutting concerns. The core concern is the business, and less relevant is the crosscutting concern. Such as: log, permission authentication, transaction. AOP serves to separate the various concerns of a software system.

Two ways to proxy AOP: Spring provides two ways to generate proxy objects: JDKProxy and Cglib. The default strategy is to use JDKProxy if the target class is an interface, and Cglib if not