Deep understanding of Spring's two characteristics (IOC and AOP) < go >

Source: Internet
Author: User
Tags aop

A blogger's blog read an article explaining Spring's two core IOC and AOP, to be forwarded and hopefully help more people.

Original address: 68946881

As we all know, the core feature of spring is the IOC and AOP,IOC (inversion of control), namely "inversion of controls"; AOP (aspect-orientedprogramming), which is "aspect-oriented programming".

IOC:IOC, another term called DI (Dependency injection), which is dependency injection. It is not a technical realization, but a design idea. In any program project that has practical development significance, we use many classes to describe their unique functionality and to accomplish specific business logic through collaboration between classes and classes. At this point, each class is responsible for managing the references and dependencies of the classes with which it interacts, and the code becomes unusually difficult to maintain and highly coupled. And the IOC's appearance is to solve this problem, we pass the creation and coordination of these interdependent objects through the IOC to the spring container to deal with, each object only need to pay attention to its own business logic relationship. In this perspective, the way in which dependent objects are obtained is reversed, turning into a spring container that controls how objects get external resources (including other objects and file data, etc.).

Explain the IOC's dependency injection through a small, easy-to-understand story:

One day, you are sick, but you do not know what is the disease, you only know that you have a headache, cough, weak body. This time you decided to go to the pharmacy to buy medicine, pharmacies have many kinds of drugs, only the treatment of headache there are dozens of kinds of, as well as Western medicine and other differences. Then you read the manual, choose a box of your own feel the most able to treat their own symptoms of medicine, pay money to take medicine, look forward to better soon.
But this process, for a patient, is too hard. Headache, cough, general weakness, but also a look at the drug instructions, a comparison of which medicine is better, is simply too tired. At this time, you decided to go directly to the hospital to see the doctor.
The doctor has checked you to see what your condition is and what causes it, and the doctor knows exactly what medications are available to treat your illness and can be screened according to your own situation. It takes just more than 10 minutes for you to get the right medicine, which saves time and effort.

In the above example, the IOC acts as a doctor, collects your requirements and gives you the right medicine. You are the object, and the medicine is the external resource you need. Through the doctor, you don't have to go to the medicine, but you pass the medicine to you through the doctor. This is the essence of the entire IOC.

AOP: Aspect-oriented programming, often defined as a technology that drives software systems to realize the separation of concerns. The system is made up of many different components, each of which is responsible for a specific function. In addition to implementing their core functions, these components often carry additional responsibilities. Core services such as logging, transaction management, and security are often incorporated into components of their own core business logic. These system services are often referred to as crosscutting concerns because they span multiple components of the system.

The concept of AOP is not as good as an example of an IOC instantiation, and now we're talking about what the AOP is specifically about, in terms of a specific implementation in a system.

We take the example of a transaction control that is commonly used in the system. In the process of operating the database, it is unavoidable to take into account the transaction-related content. If you create a new transaction manager in each of these methods, it is undoubtedly a serious coupling and intrusion to the code. In order to simplify our development process (in fact all the implementations of spring are designed to simplify the development process), the transaction-related code needs to be pumped out as a separate module. With AOP, verify that each action database method is a connection point that makes up a facet. When the program runs to one of the tangent points, we weave the transaction management module into the object, through the notification function, complete the implementation of the entire transaction control. In this way, all the methods of the operational database do not need to be concerned about the content of transaction management, only need to focus on the implementation of their own business code. All transactional control-related content is implemented in an AOP manner. Simplifies the content of the code, decoupling the complex content of the target object, separating the business logic from the crosscutting concerns.

Introduction to the relevant terminology in AOP:

      • Notifications: Notifications Define the concepts of what facets are and when to use them. There are 5 types of notifications that can be applied to Spring facets:

        • Pre-notification (before): invokes the notification function before the target method is called.
        • Post notification (after): A notification is called after the target method completes, and it does not care what the output of the method is.
        • return notification (after-returning): A notification is invoked after the target method executes successfully.
        • Exception Notification (after-throwing): A notification is invoked after the target method throws an exception.
        • Surround Notification (Around): Notifies the method that the notification has been wrapped, before the method call is notified and after the call executes the custom behavior.
      • Connection point: A point in which a slice can be inserted during application execution.

      • Tangency point: A tangent defines where the tangent plane is to be woven into one or more connection points.
      • Facets: A combination of notifications and pointcuts. The notification and tangent points together define the entire contents of the slice.
      • Introduction: Introduction allows us to add new methods or properties to an existing class.
      • Weaving: Is the process of applying facets to the target object and creating a new proxy object. Facets are woven into the target object at the specified connection point. There are multiple points in the life cycle of the target object that can be woven into:
        • Compile time: When the target class is compiled, the facets are woven into. This approach requires a special compiler. AspectJ's weaving compiler is the way in which the facets are woven in this manner.
        • Class load period: Facets are woven into the target when it is loaded into the JVM. This approach requires a special ClassLoader (class loader) that enhances the byte code of the target class before the target class is introduced to the application.
        • Run time: Slices are woven into the application when it runs to a certain point in time. In general, when you weave a slice, the AOP container dynamically creates a proxy object for the target object. The SPRINGAOP is woven into the plane in this way.

Deep understanding of Spring's two characteristics (IOC and AOP) < go >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.