Spring framework (4) --- explanations of AOP, spring --- aop

Source: Internet
Author: User

Spring framework (4) --- explanations of AOP, spring --- aop

Introduction to AOP

I have to say that it is a little difficult to understand this Aop at the beginning. It is mainly because there are many new concepts and it may not be quickly absorbed by beginners at once, so let me explain what Aop is:

First Note: This article is not written by myself, is reproduced in: http://www.cnblogs.com/zs234/p/3267623.html very grateful to this author for such a good article, I am just a porter.

Last blog post Spring framework (3) --- IOC Assembly Bean (annotation method) focuses on bean knowledge. Next we will continue to explain another core AOP (Aspect Oriented Programming ), this 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. 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) {this.tar getObject = targetObject; return Proxy. newProxyInstance (targetObject. getClass (). getClassLoader (), targetObject. getClass (). getInterfaces (), this);} public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {command (); Object ret = Method. invoke (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:

From this example, we can see that we have not written two commands by the trainer !, But it can be run twice. Does this improve the code efficiency.

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. Finally, I would like to thank the original author of this article. Thank you!

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.