Why do I need AOP?
Take a look at the code first:
PackageCom.cn.spring.aop.helloworld;//interface classes for subtraction Public InterfaceArithmeticcalculator {intAddintIintj); intSubintIintj); intMulintIintj); intDivintIintj);}
PackageCom.cn.spring.aop.helloworld;//Implementation Class Public classArithmeticcalculatorimplImplementsArithmeticcalculator {@Override Public intAddintIintj) {intresult = i +J; returnresult; } @Override Public intSubintIintj) {intresult = i-J; returnresult; } @Override Public intMulintIintj) {intresult = i *J; returnresult; } @Override Public intDivintIintj) {intresult = I/J; returnresult; }}
Here's the problem:
Now you need to track what is happening during program execution?
The code is as follows:
PackageCom.cn.spring.aop.helloworld;//Implementation Class Public classArithmeticcalculatorimplImplementsArithmeticcalculator {@Override Public intAddintIintj) {System.out.println ("The method Add begins with[" + i + "," + j + "]"); intresult = i +J; System.out.println ("The method Add ends with[" + i + "," + j + "]"); returnresult; } @Override Public intSubintIintj) {System.out.println ("The method Sub begins with[" + i + "," + j + "]"); intresult = i-J; System.out.println ("The method sub ends with[" + i + "," + j + "]"); returnresult; } @Override Public intMulintIintj) {System.out.println ("The method Mul begins with[" + i + "," + j + "]"); intresult = i *J; System.out.println ("The method Mul ends with[" + i + "," + j + "]"); returnresult; } @Override Public intDivintIintj) {System.out.println ("The method Div begins with[" + i + "," + j + "]"); intresult = I/J; System.out.println ("The method div ends With[" + i + "," + j + "]"); returnresult; }}
If you also want the calculator's subtraction to handle only integer operations, but also need to add code in the Arithmeticcalculatorimpl implementation class to verify, each add a need to add a similar code.
Makes code redundant and poorly maintained.
How to solve it with AOP?
AOP is the principle of using the proxy design pattern: Wrapping an object with a proxy and then replacing the original object with that proxy object. Any calls to the original object are passed through the proxy. The proxy object determines whether and when the method call is forwarded to the original object.
Let's take a look at how the agent is implemented:
PackageCom.cn.spring.aop.helloworld;//interface classes for subtraction Public InterfaceArithmeticcalculator {intAddintIintj); intSubintIintj); intMulintIintj); intDivintIintj);}
PackageCom.cn.spring.aop.helloworld;//Implementation Class Public classArithmeticcalculatorimplImplementsArithmeticcalculator {@Override Public intAddintIintj) {intresult = i +J; returnresult; } @Override Public intSubintIintj) {intresult = i-J; returnresult; } @Override Public intMulintIintj) {intresult = i *J; returnresult; } @Override Public intDivintIintj) {intresult = I/J; returnresult; }}
PackageCom.cn.spring.aop.helloworld;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;Importjava.util.Arrays; Public classArithmeticcalculatorloggingproxy {//the object to be proxied Privatearithmeticcalculator Target; PublicArithmeticcalculatorloggingproxy (arithmeticcalculator target) { This. target =Target; } Publicarithmeticcalculator Getloggingproxy () {Arithmeticcalculator proxy=NULL; //which class loader is responsible for loading the proxy objectClassLoader loader =Target.getclass (). getClassLoader (); //the type of the proxy object, that is, which methodsClass[] Interfaces =NewClass[]{arithmeticcalculator.class}; Invocationhandler Handler=NewInvocationhandler () {/** * * @paramproxy object being returned, in general, the object is not used in the Invoke method *@parammethod that is being called *@paramwhen args invokes a method, the parameter passed in *@return * @throwsThrowable*/@Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {String methodName=Method.getname (); System.out.println ("The methed" + MethodName + "begins with" +arrays.aslist (args)); Object result=Method.invoke (target, args); System.out.println ("The methed" + MethodName + "End with" +arrays.aslist (args)); returnresult; } }; Proxy=(arithmeticcalculator) proxy.newproxyinstance (loader, interfaces, handler); returnproxy; }}
PackageCom.cn.spring.aop.helloworld;/*** Created by JECYHW on 2015/6/20.*/ Public classMain { Public Static voidMain (string[] args) {arithmeticcalculator target=NewArithmeticcalculatorimpl (); Arithmeticcalculator Proxy=NewArithmeticcalculatorloggingproxy (target). Getloggingproxy (); intresult = Proxy.add (1, 2); System.out.println ("-" +result); Result= Proxy.sub (1, 2); System.out.println ("-" +result); Result= Proxy.mul (1, 2); System.out.println ("-" +result); Result= Proxy.div (1, 2); System.out.println ("-" +result); }}
Spring AOP Basics:
- AOP (aspect-oriented programming for slicing) is a new methodology that complements traditional OOP (object-oriented programming Object-oriented programming)
- The main programming objects of AOP are facets (aspect), while facets are modular crosscutting concerns.
- When you apply AOP programming, you still need to define public functionality, but you can clearly define where and how this function is applied, without having to modify the affected classes. This way the crosscutting concerns are modularized into special objects (facets).
- The benefits of AOP: The logic of each thing is in one place, the code is not scattered, it is easy to maintain and upgrade, the business module is more concise and contains only the core business code.
- Facets (Aspect): Special objects that are modularized by crosscutting concerns (functions that span multiple modules of the application)
- Notification (Advice): The work that the slice must complete
- Target: The object being notified
- Proxy: An object created after the notification is applied to the target object
- Connection point (Joinpoint): A specific location where a program executes: Before a method call to a class, after a call, after a method throws an exception, and so on, the connection point is determined by two information: The program execution point represented by the method, and the position represented by the relative point, such as arithmeticcalculator# The connection point before the Add () method executes, the execution point is arithmeticcalculator#add (), and the method is the position before the method executes
- Tangency Point (pointcut): Each class has multiple connection points: for example, all the methods of Arithmeticcalculator are actually connection points, that is, the connection point is an objective object in the program class. AOP navigates to a specific connection point through a pointcut. Analogy: A connection point is equivalent to a record in a database, and a tangent is equivalent to a query condition. Tangency and connection points are not a one-to-one relationship, a pointcut matches multiple connection points, and the pointcut is described by the Org.springframework.aop.Pointcut interface, which uses classes and methods as query criteria for connection points.
15Spring AOP Basics