This article is based on the College of geek’s “Spring IOC” video course.
Spring is a lightweight control inversion (IOC) and aspect oriented (AOP) container framework that is primarily designed to address the complexity of enterprise application development.
- Function: use the basic JavaBean instead of EJB
- Scope: applies to any Java application
- Core: IOC and AOP
layered, one-stop, lightweight open source framework.
Spring benefits Development
IOC of Spring
The difference between the 1. IOC development model and the traditional development model:
- IOC (control inversion) is the core of spring. For the spring framework, the spring is the complex control of the object’s life cycle and the relationship between objects.
- For example:
traditional development model: objects depend on each other.
in an object, if you want to use another object, you must get it, own new one, or query one from JNDI, after use, you must destroy the object.
IOC development pattern: IOC containers arrange dependencies between objects.
is similar to finding objects through matchmaking, and all classes of
are told in the spring container to describe spring’s own description of the object that is needed. Spring will give you what you need at the right time when the program is running and give you something else that needs you. The creation and destruction of all classes are controlled by spring.
controls the object life cycle no longer by referencing his object, but by the spring container. For a specific object, before, it controls other objects, and now all objects are controlled by spring. This is called inversion of control.
2.IOC theoretical knowledge:
- The complex system is decomposed into the object of mutual cooperation, these objects through the package after the implementation, it is transparent to the outside, thereby reducing the complexity of the problem, and can flexibly be reused and extended.
- In short, the decoupling between objects with dependencies is achieved by means of third parties.
IOC container control relationship between all objects, which makes the IOC container has become the key to the core of the whole system, similar to the role of adhesive, all objects in the system together play a role.
, IOC theoretical background
Taking the middle of the IOC container off is what we need to accomplish with the whole system. At this point, there is no coupling between the four ABCD objects, and there is no connection. Thus, when we implement A, we don’t need to consider several other objects. So developers just have to implement their own classes.
When IOC containers are not imported, objects depend on each other. When the object A needs to be initialized or run to a certain point, you have to create the object B vividly, or use the existing object B to control it in your hand. When
introduces the IOC container, there is no direct link between object A and object B. When the object A runs to the object B, the IOC container will actively inject an object B into the object A where the object B is required.
the entire process, the object A control of the object B, from the initiative into a passive, control upside down.
3. IOC dependency injection DI
- Inversion of control: the process of obtaining dependent objects is reversed.
- After controlling inversion, the process of obtaining dependency objects becomes managed by the IOC container actively by itself. So another name for IOC is dependency injection. In fact, this gives you a way to implement IOC containers, that is, to inject.
- Computer hosting versus USB devices – a common example of dependency injection. The
computer hosts the resources of the USB device through the USB interface. When the computer needs to read a resource, I will take the initiative to connect the external device to the host via the USB interface. Computers don’t need to care about external connections, U disk or hard disk or something else?. The required resources are read directly through the USB interface. In the process, I play the role of a IOC container. I am responsible for the creation and destruction of all objects. Each object itself is not managed. When the computer host needs to read the file, I hook it up with the external equipment it needs.
- A IOC container is an object manufacturing plant. If you need anything, he sends it to you. You can use whatever you want. There is no need to care about how objects are generated, nor do they need to be concerned about how objects are destroyed. The IOC container implements relationships between components from the inside of the program to the external containers, which are dynamically injected into the components during the run of the program by relying on dependencies between components.
The benefits of 4. IOC
IOC does not constitute a strong intrusion into business objects during programming. After using IOC, objects have better feasibility, reusability and extensibility.
- Reducing the coupling between components,
what is the advantage of using USB over an internal hard disk? The
USB device as an external device in the host computer is inserted into the host before, and no relation between the host, we only to be inserted into the USB host interface, both took place in connection with the correlation. No matter what happens to either of them, it does not affect the other. This is embodied in software engineering, that is, maintainability. Easy for unit testing, easy to debug programs and diagnose faults, each class in the code can be tested separately, without affecting each other, as long as the function is guaranteed to be correct. This is the advantage of code low coupling or no coupling.
- Another advantage of improving the efficiency and quality of the
host and the USB device is that the manufacturer of the host machine and the manufacturer of the USB device can be two. The only thing that needs to be observed is the USB standard. In software development, is reflected in each person, the development team will only need to be concerned with the implementation of business logic itself, not to be concerned about the progress of the work of others, because of their own tasks and others have no relationship, can be individually tested, does not depend on others tasks. In large and medium scale development projects, a large task can be divided into several small tasks, with a clear division of labor and efficiency.
- Unified standards increase the reusability of modules such as USB devices that can migrate to a computer host, migrate to a DV, and migrate to any
enabled device that can be reused USB. In software engineering, this is reusability. In the development process, the common, commonly used components are independent, and reused to other parts of the project or other projects, of course, this is also object-oriented features. IOC improves the reusability of modules and meets the implementation of interface standards, and can migrate to modules that support this standard.
- The module has a hot swap feature,
, similar to USB plug and play. IOC makes object generation into an external file, that is, a XML file, so that the replacement component is very simple.
5., IOC’s popular understanding
IOC control inversion: the control of creating object instances is stripped from the code control to the IOC container control, which is actually controlled in the XML file, with a focus on the principle.
DI dependency injection: when creating an object instance, inject attribute values or other object instances for that object, with a focus on implementation.
AOP of spring
1. what is AOP?
The basic concept of Aspect-Oriented – Programming, aspect oriented programming technology, the application system is divided into two parts: the core business logic, and universal logic (called lateral aspect), such as permanent management. Transaction management. Security management, log management and debugging management, etc.. In spring, allowing separation of business logic and system level services for cohesive development. The application object only completes the business logic.
– the relationship between AOP and AOP – OOP is a useful complement to object oriented OOP, a continuation of OOP, and an important part of spring. OOP mainly considers entities, attributes, classes, and studies a static field. OOP considers the dynamic process of the program, extracts the facets in the business process, and studies the dynamic fields. The main function of
– AOP is mainly for system level functions. For example, logging, performance statistics, security control, transaction processing, exception handling, and so forth. The main
— the intention of AOP logging, performance statistics, security control, transaction processing, exception handling and other system level functions, separated from the business logic, through the separation of these behaviors, hope they can be independent to the non directive business logic method, time and change the behavior, does not affect the business logic code. That is, AOP modular the commonly used services and uses them in a declarative way to other business components. This allows each business component to care only about its business logic without having to understand the common service components. Higher cohesion is guaranteed.
AOP is the next halo of software development that can effectively prevent code clutter. The spring framework is a lightweight AOP technology that can be used in Java code without the use of a precompiled or other language tag.
The existence value of 2. AOP
The system has many different components, each of which is responsible for specific functions. We want each component to care only about its own core capabilities.
but there are some components in the system, such as log modules, transaction modules, security modules, which will be integrated into other core business logic components more frequently. These commonly used components can be scattered among the other components. This can lead to two problems: if these common service components change frequently, we need to modify them among multiple other components. In addition, component code is confusing because of the insertion of other service components that are unrelated to the core business.
, as shown in Figure 1 above, multiple methods use the same code segment, which is developed by copying and pasting. If the code segment needs to be modified, then the work is repeated and huge.
pictured in Figure 2, we will this code package into a method, the three code just call this method are good, and if you need to modify the color code, only need to modify a good. Greatly reduce the complexity of the late maintenance of software. This approach can deal with most application scenarios.
but there are some special cases, for example, the application requires three methods to completely separate from the dark method. In software development, code needs to be constantly modified, and new requirements can be put forward at any time. Faced with a change in requirements, if the definition of a new method, and then modify the three methods, adding new definitions of the method, the workload is also great. We want to define a method that can automatically call this method without the need to display calls in three methods. This way of doing this is AOP technology.
The analysis of the principle of 3. AOP
The AOP is actually implemented by the proxy class of the target class, containing all the methods of the target object. But there is a difference between the methods in the AOP agent and the target object, and the AOP method adds the enhancement process to the particular pointcut and callback the target object.
For AOP programming, the need for program ape participation in three parts:
- Define common business components
- Defines a pointcut, a pointcut that can crosscut multiple business components.
- Defining enhancement processing enhancement processing is the processing action that is embedded in the common service component in the AOP framework.
so, the key to AOP programming is defining pointcut and defining enhancement processing. Once the appropriate pointcut is defined and the enhancement process is defined, the AOP framework automatically generates the AOP agent. The implementation principle of
AOP, in brief, is that the AOP framework is responsible for the dynamic generation of AOP proxy classes, consisting of methods that enhance and callback target objects.
Key concepts of 4. AOP
The spring official document explains some of the key concepts of AOP:
- The cut -Aspect section is the modularization of concerns across multiple classes. Transaction management is a good example of crosscutting relationships in J2EE applications. In Spring AOP, the section is implemented using a conventional class (schema based method) or a regular class annotated with the @aspect annotation (@aspectj style).
- The connection point -Join Point connection point is a specific point during the execution of a program, such as when executing a method or handling an exception. In Spring AOP, the join point always indicates the execution of the method.
- Notify -Advice notification of actions taken at an aspect of a particular connection point. Different types of notifications include “arround”, “before” and “after” notifications. Many AOP frameworks, including Spring, use interceptor as a notification model to maintain a string of interceptors around the connection point.
- The pointcut, -Point, Cut pointcut, the predicate that matches the connection point. The notification is associated with the pointcut expression and runs on any join point that the pointcut matches (for example, the execution of a method with a given name). The concept that the pointcut expression matches the pointcut expression is the core of the AOP, and Spring uses the AspectJ pointcut expression language by default.
- Introduce -Introduction import to declare additional methods or fields for a type. Spring AOP allows you to introduce new interfaces (and the corresponding implementation) to any object that is notified (i.e., the object being represented). For example, you can use an introduction to make bean implement a IsModified interface to simplify caching. (introduced in the AspectJ community, known as cross type declarations). )
- The target object -Target Object, the target object is notified by one or more sections, also known as the proposed object. Since Spring AOP is implemented using the runtime agent, this object will always be a proxy object.
- The AOP proxy -AOP Proxy AOP agent, an object created by the AOP framework, is designed to implement the section contract (notification, method execution, and so on). In the Spring framework, the AOP agent will be a JDK dynamic agent or CGLIB agent.
- Weave into -Weaving to link the sections to other application types or objects to create an object to be notified. This can be done at compile time (for example, using the AspectJ compiler), when loaded, or at run time. Like other pure Java AOP frameworks, Spring AOP is woven at run time.
5., AOP’s popular understanding
Where there is something wrong, please correct me!