Spring AOP and Spring AOP

Source: Internet
Author: User
Tags mul

Spring AOP and Spring AOP

Introduction to AOP
AOP (Aspect-Oriented Programming): a new methodology that complements traditional OOP
The main programming object aspect of AOP, while the cross-section modular cross-cutting concerns
When applying AOP programming, you still need to define public functions, but you can clearly define where the function is to be applied and how it is applied without modifying the affected classes. In this way, the cross-cutting concerns are modularized into special objects (slices.
Benefits of AOP
Each transaction logic is located in one location, and the code is not scattered, so it is easy to maintain and upgrade.
The business module is more concise and only contains the core business code

AOP terminology
Aspect (Aspect): A special object that is modularized by a cross-concern (the function that spans multiple modules of an application ).
Advice
Target: the object to be notified.
Proxy: the object created after the notification is applied to the target object
Joinpoint: a specific position of a program execution, such as before, after, or after a method is called. The connection point has two pieces of information to determine: The execution point represented by the method, and the orientation indicated by the relative point. For example, the connection point before the ArithmeticCalculator # add () method is executed. The execution point is ArithmeticCalculator # add (). The orientation is the position before the method is executed.
Pointcut: each class has multiple connection points. For example, all methods of ArithmeticCalculator are connection points, that is, the connection points are objective transactions in the program class. AOP locates a specific connection point by cutting points. Analogy: the connection point is equivalent to a record in the database, and the cut point is equivalent to a query condition. The cut point is described through the org. springframework. aop. Pointcut interface, which uses classes and methods as the query condition for the connection point.

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following is a simple calculator to explain the background of AOP.

Interface: ArithmeticCalculator. java, which defines the addition, subtraction, multiplication, and division methods.

 1 package com.yl.spring.aop.helloworld; 2  3 public interface ArithmeticCalculator { 4      5     int add(int i, int j); 6     int sub(int i, int j); 7      8     int mul(int i, int j); 9     int div(int i, int j);10 }

Implementation class: ArithmeticCalculatorImpl. java implements basic addition, subtraction, multiplication, division

 1 package com.yl.spring.aop.helloworld; 2  3 public class ArithmeticCalculatorImpl implements ArithmeticCalculator { 4  5     @Override 6     public int add(int i, int j) { 7         int result = i + j; 8         return result; 9         10     }11 12     @Override13     public int sub(int i, int j) {14         int result = i - j;15         return result;16     }17 18     @Override19     public int mul(int i, int j) {20         int result = i * j;21         return result;22     }23 24     @Override25     public int div(int i, int j) {26         int result = i / j;27         return result;28     }29 30 }

Now, the above two pieces of code implement the basic calculator. Add logs before and after calculation for new requirements.

The simplest implementation is to add the Log Code before and after the calculation statement of each method, that is, the output statement. The specific implementation is as follows:

ArithmeticCalculatorLoggingImpl. java

 1 package com.yl.spring.aop.helloworld; 2  3 public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator { 4  5     @Override 6     public int add(int i, int j) { 7         System.out.println("the method add begin with[" + i + ", " + j + "]"); 8         int result = i + j; 9         System.out.println("the method add end with " +result);10         return result;11         12     }13 14     @Override15     public int sub(int i, int j) {16         System.out.println("the method sub begin with[" + i + ", " + j + "]");17         int result = i - j;18         System.out.println("the method sub end with " +result);19         return result;20     }21 22     @Override23     public int mul(int i, int j) {24         System.out.println("the method mul begin with[" + i + ", " + j + "]");25         int result = i * j;26         System.out.println("the method mul end with " +result);27         return result;28     }29 30     @Override31     public int div(int i, int j) {32         System.out.println("the method div begin with[" + i + ", " + j + "]");33         int result = i / j;34         System.out.println("the method div end with " +result);35         return result;36     }37 38 }

However, the above implementation method increases the complexity of the business module, and it is not convenient to modify the log code.

Next we will introduce a new method, that is, dynamic proxy. The specific implementation is as follows:

ArithmeticCalculatorLoggingProxy. java

1 package com. yl. spring. aop. helloworld; 2 3 import java. lang. reflect. invocationHandler; 4 import java. lang. reflect. method; 5 import java. lang. reflect. proxy; 6 import java. util. arrays; 7 8 import org. springframework. beans. propertyeditors. classArrayEditor; 9 10 public class counter {11 // the object to be proxy 12 private ArithmeticCalculator target; 13 14 public ArithmeticCalculatorLoggingProxy (ArithmeticCalculator target) {15 this.tar get = target; 16} 17 18 public ArithmeticCalculator getLoggingProxy () {19 ArithmeticCalculator proxy = null; 20 21 // which class loader is responsible for loading 22 ClassLoader loader = target. getClass (). getClassLoader (); 23 // proxy object type, that is, which of the following methods is available: 24 Class [] interfaces = new Class [] {ArithmeticCalculator. class}; 25 // when calling the method in the proxy object, code 26 InvocationHandler h = new InvocationHandler () {27/** 28 * proxy: the proxy object being returned. Generally, this object is not used in the invoke method 29 * method: the method being called 30 * args: when the method is called, the input parameter 31 */32 @ Override33 public Object invoke (Object proxy, Method method, Object [] args) 34 throws Throwable {35 // This sentence may cause loop calls, eventually memory overflow 36 // System. out. println (proxy. toString (); 37 38 39 String methodName = method. getName (); 40 // log 41 System. out. println ("the method" + methodName + "begin with" + Arrays. asList (args); 42 // execute this method 43 Object result = method. invoke (target, args); 44 // log 45 System. out. println ("the method" + methodName + "end with" + result); 46 return result; 47} 48}; 49 50 proxy = (ArithmeticCalculator) Proxy. newProxyInstance (loader, interfaces, h); 51 52 return proxy; 53} 54 55}

So far, the Implementation of Dynamic proxy is complete.

Test class:

1 package com. yl. spring. aop. helloworld; 2 3 public class Main {4 public static void main (String [] args) {5 6 // * // test log 7 ArithmeticCalculator arithmeticCalculator = null; 8 arithmeticCalculator = new ArithmeticCalculatorLoggingImpl (); 9 10 int result = arithmeticCalculator. add (1, 2); 11 System. out. println ("-->" + result); 12 13 result = arithmeticCalculator. div (4, 2); 14 System. out. println ("-->" + result); */15 16 // dynamic proxy test 17 ArithmeticCalculator target = new ArithmeticCalculatorImpl (); 18 ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy (target ). getLoggingProxy (); 19 20 System. out. println (proxy. getClass (). getName (); 21 22 int result = proxy. add (1, 2); 23 System. out. println ("-->" + result); 24 25 result = proxy. div (4, 2); 26 System. out. println ("-->" + result); 27} 28}

For specific use of AOP, refer to the subsequent articles in this series ......

 

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.