We are in the process of using the spring framework in order to use IOC, dependency injection, and AOP, aspect-oriented programming, these two are spring souls.
The main design patterns used are Factory mode and proxy mode.
The IOC is a typical factory model, with Sessionfactory to inject instances.
AOP is the embodiment of a typical proxy model.
Proxy mode is a common Java design pattern, his characteristic is that the proxy class and the delegate class have the same interface, the proxy class is mainly responsible for the delegate class preprocessing messages, filtering messages, forwarding messages to the delegate class, and post-processing messages. There is usually an association between the proxy class and the delegate class, and the object of a proxy class is associated with an object of a delegate class, and the object of the proxy class does not actually implement the service, but instead provides a specific service by invoking the related method of the object of the delegate class.
Spring's IOC container is the core of spring, and spring AOP is an important part of the spring framework.
In traditional program design, when the caller needs to be assisted by the caller, it is usually the caller who creates an instance of the callee. But the work of creating the callee in spring is no longer done by the caller, so the inversion of control (IoC); the work of creating a callee instance is usually done by the spring container, which is then injected into the caller and therefore also referred to as Dependency injection (DI), and dependency injection and control inversion are the same concept.
Aspect-oriented programming (AOP) is to consider the program structure from another perspective, and to refine object-oriented programming (OOP) by analyzing the focus of the program structure. OOP breaks down an application into objects of all levels, while AOP decomposes the program into multiple facets. Spring AOP implements only the method-level connection points, and in the Java EE application, it is sufficient for the AOP to intercept the method-level operations. In spring, the future makes it easy for the IOC to use robust, flexible enterprise services, using the spring AOP implementation to establish a connection between IOC and Enterprise services.
IOC: Control inversion is also called dependency injection. Using the factory model
To give the object to container management, you only need to configure the appropriate bean in the spring configuration file, and set the related properties so that the spring container generates the class's instance object and manages the object. When the spring container is started, spring will initialize the beans you configured in the config file, and then, when you need to call, assign the beans that are already initialized to the class you need to call them (assuming the class name is a), The method of allocation is to call a setter method to inject, without needing you to new these beans in a.
Note: In the interview, if there is a condition, draw, it seems you understand more.
AOP: Plane-oriented programming. (Aspect-oriented programming)
AOP can be said to complement and improve OOP. OOP introduces concepts such as encapsulation, inheritance, and polymorphism to create an object hierarchy that simulates a collection of public behavior. When we need to introduce public behavior to scattered objects, oop seems powerless. In other words, OOP allows you to define relationships from top to bottom, but it is not appropriate to define left-to-right relationships. such as logging capabilities. Log code is often spread horizontally across all object hierarchies, and has nothing to do with the core functionality of the objects it spreads to. In OOP design, it leads to a lot of duplication of code, which is not conducive to the reuse of each module.
The cross-business logic in the program (such as security, logs, transactions, etc.) is encapsulated into a slice and then injected into the target object (the specific business logic).
The implementation of AOP technology, mainly divided into two categories: first, the use of dynamic Agent technology, the use of interception of messages to decorate the message to replace the original object behavior, the second is the use of static weaving, the introduction of a specific syntax to create "aspects", so that the compiler can be woven into the relevant "aspects" The code.
Simply explain, for example, that you want to add a print ' hello ' function to all classes in your Biz layer, and you can do it with AOP. First you write a class to write a class method, the method is implemented to print ' Hello ', and then the IOC class ref= "biz.*" so that each class is injected into the implementation.
Detailed explanations of IOC and AOP in spring