In-depth explanation of spring (iii) AOP

Source: Internet
Author: User

The previous blog post goes deep into spring (2)
In the detailed description of IOC, I briefly introduced IOC in the core content of the Spring framework. Next we will continue to explain another core AOP (Aspect Oriented Programming), that is, Aspect-Oriented Programming.

1. Oop Review

Before introducing AOP, let's review the familiar OOP (Object Oriented Programming ). Oop is mainly used to realize reusability, flexibility, and scalability of programming. Its features are inheritance, encapsulation, polymorphism, and abstraction. Oop focuses on the programming architecture and emphasizes the hierarchical relationship between classes.

2. Oop Defects

To better describe the concept of OOP, let's talk about an OOP instance, focusing on the defects in OOP, so as to better understand the relevant content of AOP.

First look at the following figure:



The preceding figure has three classes: dog, cat, and duck. Both of them have a run method. According to the OOP design philosophy, we can easily think of Abstracting an animal parent class, and let these three subclasses inherit the animal parent class. The following figure shows the design:


In the idea of OOP, we will use a large number of similar programming methods to abstract, inherit, encapsulate and polymorphism classes to realize the reusability, flexibility and scalability of programming. However, such programming still has some limitations. Sometimes, oop cannot solve the problems we encounter in actual development. To illustrate this problem, see the figure below:



As shown in the figure above, we cannot find any problems at the moment. For ease of understanding, I will explain the implementation process of the class graph above. Description:The circus has a performance puppy that can run and dance, but before it completes the run and dance, it must be after receiving a command from the trainer, after running and jumping at the same time, the trainer will reward the response, such as a piece of meat.

After learning about the implementation process, let's take a look at the specific code.

Public class dog {public void run () {system. Out. println ("the trainer sends a command! ") System. Out. println (" the puppy starts running! "); System. Out. pringln (" Rewards ");} public void jump () {system. Out. println (" give a command! ") System. Out. println (" the puppy starts to jump! "); System. Out. pringln (" Rewards ");}}

By taking a closer look at the above code, we can see that there are some identical content in the run and jump methods (commands issued by the trainer and rewards), which cannot be completely abstract, that is, it cannot be handled according to the OOP programming idea. Similar situations may also occur in many areas of our programming, such as logging, Performance Statistics, security control, transaction processing, Exception Processing, and so on. But how can this problem be solved? This introduces the idea of AOP programming.


3. Introduction to AOP

AOP is short for Aspect Oriented Programming, that is, Aspect-oriented programming (also called Aspect-Oriented ), it is a kind of technology that can dynamically and uniformly add functions to programs without modifying the source code through the pre-compilation method and runtime dynamic proxy.


4. AOP implementation example

To better understand how to implement AOP, We will optimize the above Code.

The first is the dog class.

Public interface animal {public void run (); Public void jump ();} public class dog implements animal {public void run () {system. Out. println ("puppies start running! ");} Public void jump () {system. Out. println (" the puppy starts to jump! ");}}

Compared with the previous code, we can see that we extracted the content about the trainer from run and jump. Next, how can we add the actions about the animal trainer to the program during the program running? This is the core technology dynamic proxy (Dynamic proxy) of the AOP implementation we used this time ). The Code is as follows:

Public class myproxy implements invocationhandler {private object targetobject; public object createproxyinstance (Object targetobject) implements this.tar GetObject = targetobject; return proxy. newproxyinstance (targetobject. getclass (). getclassloader (), targetobject. getclass (). getinterfaces (), this);} public object invoke (Object proxy, method, object [] ARGs) throws throwable {command (); object ret = method. in Voke (targetobject, argS); Award (); return ret;} private void command () {system. Out. println ("the animal trainer sends a command! ");} Private void Award () {system. Out. println (" the trainer will reward you! ");}}

After the above Code is implemented, how can we call it? The reference code is as follows:

public class Client {public static void main(String[] args) {MyProxy hander = new MyProxy();Animal dog = (Animal)hander.createProxyInstance(new Dog());dog.run();dog.jump();}}

The execution result is as follows:



The example of AOP programming is completed. Next we will review the related concepts of AOP and OOP.


5. Relationship between AOP and OOP

Oop abstracts and encapsulates entities (dog, cat, duck) and their properties and actions (run) in the business processing process to achieve clearer and more efficient logical unit division. AOP extracts the run or jump plane (command and award) in the business processing process. It faces a certain step or stage in the processing process, in order to obtain the isolation effect of low coupling between different parts in the logical process. These two design ideas are essentially different in terms of goals.

This article has basically explained the concept of AOP so far. As for how to use AOP in spring, I will explain an instance in the future blog, in addition, we have not finished talking about AOP this time. There are still many concepts that I will explain in the future! Coming soon!


Too much water, you just click on, and then give me some comments! Yes. Click the top of the page and give me some comments! Thank you for your cooperation. Your support is my greatest motivation!

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.