Spring (iii) AOP

Source: Internet
Author: User
Tags abstract aop exception handling
The last blog. Spring (ii) in the IOC, I briefly introduced the IOC in the core content of the spring framework, and then we went on to another core AOP (Aspect oriented programming), namely, tangent-oriented programming.

1. Oop ReviewBefore introducing AOP, let's review the OOP (Object oriented programming) that everyone is familiar with. OOP is primarily designed to achieve reusability, flexibility, and extensibility of programming. Several of its characteristics are inheritance, encapsulation, polymorphism, and abstraction. OOP focuses on the programming architecture, emphasizing the hierarchical relationship between classes.

2. Oop defectsTo better illustrate the concept of OOP, let's talk about an example of OOP that focuses on what bugs are in oop to better understand the relevant content of AOP.

Let's look at one of the following pictures:



The above picture has three classes: Dog,cat and duck, they all have a way to run. According to Oop's design philosophy, it is easy to think of abstracting a animal parent class, while allowing these three subclasses to inherit the animal parent class. Such a design can be represented by the following diagram:



In OOP, we use a lot of programming like the above to abstract, inherit, encapsulate, and polymorphic classes to achieve the reusability, flexibility, and extensibility of programming. But this kind of programming still has certain limitation, sometimes, OOP does not solve the problem that we encounter in the actual development again. To illustrate the problem, look at the following illustration:



Seeing the picture above, we can not find any problems for the time being. For everyone to understand, next I will explain the above class diagram implementation process. The description is as follows: The Circus has a performance puppy, the puppy can run and jump, but it must complete the run and jump two before the command is issued by the trainer, while completing the running and jumping action, the trainer will give a response reward, such as a piece of meat.

Once we understand the implementation process, we'll look at the specific code.

	public class Dog {public   
		void run () {
			System.out.println ("trainer" commands.) ")
			System.out.println (" The puppy began to run. ") ");
			System.out.pringln ("Trainer give and reward");
		}  
		public void Jump () {
			System.out.println ("The trainer gives orders.")
			System.out.println ("The puppy starts jumping.") ");
			System.out.pringln ("Trainer giving and reward");}
	}

Looking closely at the code above, we can see that there are some identical contents in the Run method and the Jump method (the trainer gives commands and rewards), which cannot be completely abstracted, i.e. it cannot be handled according to OOP programming ideas. Similar situations can occur in many areas of our programming, such as logging, performance statistics, security controls, transaction processing, exception handling, and so on. But how should such a situation be solved. This introduces the idea of AOP programming.


3. Introduction to AOP

AOP is the abbreviation of Aspect oriented programming, that is, tangent-oriented programming (also called aspect-oriented), is a technology that can dynamically and uniformly add functionality to a program without modifying the source code by precompiling and running dynamic proxy implementations.


4, AOP implementation examples in order to better understand how AOP is implemented, next we optimize the above code.

First, the dog class.

	Public interface Animal {public
		void run ();
		public void jump ();
	
	public class Dog implements animal{public
		void Run () {
			System.out.println ("Puppy starts running. ");
		}	
		public void Jump () {
			System.out.println ("Puppy starts jumping. ");
		}		
	}

In contrast to the previous code, we can clearly see that we extracted the relevant content about the trainer from the run and jump, and then how do we add the action of the trainer to the program as it runs. This is the core technology dynamic proxy that we used to implement the AOP. The specific code is as follows:

import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import

Java.lang.reflect.Proxy;
	
	public class Myproxy implements invocationhandler{private Object targetObject; /** * Binds the delegate object and returns a proxy class * @param target * @return * */public object Createproxyinstance (object Targ		
		Etobject) {this.targetobject = TargetObject; Return Proxy.newproxyinstance (Targetobject.getclass (). getClassLoader (), Targetobject.getclass (). GetInterfaces ()
	, this);
	 }/** * @param proxy: Refers to the object being proxied. * @param method: Methods to Invoke * @param args: Parameters required for method invocation */public object Invoke (object proxy, method, object[] args		
		) throws Throwable {//Inject the method command () that needs to be called;
		Executes the method of the Proxied object and assigns a value to the RET object ret = Method.invoke (targetObject, args) If the method has a return value;
		Inject the method that needs to be called award ();
	return ret; private void Command () {System.out.println ("The Trainer issues the command.")
	"); } private void Award () {System.out.println ("trainer gives and rewards.
	"); }
}

After the implementation of the above code, we change how to invoke 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 results of the implementation are as follows:



An example demonstration of AOP programming is complete, and then revisit the concepts associated with AOP and OOP.


5. The relationship between AOP and OOP OOP encapsulates the entity of the business process (Dog, Cat, Duck) and its attributes and behavior (run) in an abstract package for clearer and more efficient logical unit partitioning. AOP is an extraction of the Facets (Command and award) of a business process (run or jump), which is faced with a step or stage in the process to isolate the low coupling between parts of the logical process. These two kinds of design thought have the essential difference in the goal.

At this end of the article basically the concept of AOP is explained, as for how to use AOP in spring, I will be in the next blog post for an example to explain, and this time about AOP we did not finish, there are many concepts also I will be in the future post to the specific explanation. Please look forward to it.

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.