We are in the process of using the spring framework, in order to use the IOC, dependency injection and AOP, aspect-oriented programming, these two are the soul of spring.
The main design patterns 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.
What are the advantages of spring?
1. The coupling between components is reduced, and the decoupling between each layer of software is realized.
2. Can use many services that are easy to provide, such as transaction management, message service, etc.
3. The container provides a singleton mode support
4. The container provides AOP technology, it is easy to implement such as permission interception, run-time monitoring and other functions
5. The container provides many auxiliary classes, which can accelerate the development of the application
6.spring provides integrated support for mainstream application frameworks, such as Hibernate,jpa,struts
7.spring is a low-intrusive design with very low code pollution
8. Independent of various application servers
The 9.spring di mechanism reduces the complexity of business object substitution
10.Spring is highly open and does not force applications to rely entirely on spring, and developers are free to select part or all of spring
What is a di mechanism?
Dependency Injection (Dependecy injection) and control inversion (inversion of controls) are the same concept, specifically: when a role
When another role assistance is required, the caller's instance is typically created by callers during the traditional program design process. But in spring,
The work of creating the callee is no longer done by the caller, so it is called control reversal. The work of creating the callee is done by spring and then injected into the caller
Therefore also known as dependency injection.
Spring manages objects in a dynamic and flexible manner, injecting two ways, setting injection and constructing injection.
Advantages of setting Injection: intuitive, natural
Advantages of construct injection: You can determine the order of dependencies in the constructor.
What is AOP?
Aspect-oriented programming (AOP) to improve spring's dependency injection (DI), aspect-oriented programming in spring is mainly represented in two aspects
1. To provide declarative transaction management for tangent-oriented programming
2.spring supports user-defined facets
Facet-oriented programming (AOP) complements object-oriented programming (OOP):
Object-oriented programming breaks down programs into objects of all levels, and the program runs the process into facets.
AOP considers the structure of the program from the point of view of the program, extracts the facets of the business process, and OOP is a static abstraction, and AOP is a dynamic abstraction.
is to abstract the steps in the application execution process, thus obtaining the logical division between the steps.
The two characteristics of an AOP framework are:
1. Good isolation between the various steps
2. Source code Independence
Understanding the IOC and AOP in spring