Spring learning notes what is AOP

Source: Internet
Author: User

AOP, full name of Aspect oriented programming, is often translated into Aspect-oriented programming or Aspect-oriented programming. At the beginning, many people will have a problem. What is the difference between AOP and OOP. In fact, AOP and OOP can be understood as two independent definitions at different levels. That is to say, we can't make a peering comparison between the two things. OOP focuses on objects. We use the attributes and behaviors of objects to solve real problems, AOP is used to enhance the problem-solving capability in the process of using OOP to solve problems and achieve better modularization.

 

What is a cut surface?

This section can be understood as the focus of many unrelated objects and subprograms in our program, which are usually distributed throughout the program, for example, we need to add code with security protection in many code logic (most of the Code is the same). For example, we need to record logs before and after executing a method, for example, you must implement transactions and ensure the atomicity of operations when executing methods. One of these things is what we need to focus on in our program, but it is irrelevant to the actual business logic. Before using AOP, it was dispersed in the entire program and mixed in the business logic code. If you need to change the security-related code for security considerations, you need to modify a large number of Security Processing codes originally mixed in the business logic. We need to consider a similar Aspect, that is, Aspect.

 

What can AOP do?

AOP can enhance OOP programs by enhancing modularity and reducing the coupling between aspect and business logic. You can use AOP to add functions to an object set that requires certain functions without modifying the class code of these objects. That is, we extract the parts that have nothing to do with the actual business logic but need to be concerned and dynamically add them to the business logic code through AOP. Even if the implementation mechanism and code of these Aspect are modified, only one change is required without affecting the original business logic code.

 

AOP in Spring

Unlike AspectJ, which implements AOP, AspectJ adds additional functions to the target object for compilation. The additional functions in Spring are separated from the target object, in runtime, It is dynamically added to the target object. This is usually implemented through Proxy (you can look at the Proxy mode in the design mode, Spring's AOP is implemented based on Proxy ). Because Spring does not mix the additional feature with the target object and then compile it, Spring can only add the additional feature to the object at the method level. It is equivalent to adding a specific function before or after executing a method, but not truncating and adding a function within the method body. Spring adds, modifies, or cancels specific functions to a series of specific objects at any time through the configuration file, so that the program is not affected, that is to say, these objects can run normally even if they do not use Spring AOP to add application functions. It can be understood that these objects do not know whether they have been added with some features through AOP.

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.