First, the basis of AOP
1. Basic Requirements
Requirements: Log function that records the activities that occur during program execution.
Arithmeticcalculate.java
public interface Arithmeticcalculate{public int add (int a,int b);p ublic int sub (int a,int b);p ublic int mul (int a,int b);p ublic int div (int a,int b);}
Arithmeticcalculateimpl.java
public class Arithmeticcalculateimpl implements arithmeticcalculate{@Overridepublic int add (int a,int b) { System.out.println ("The Method Add.....begin"); int result = a + B; System.out.println ("The Method Add.....end"); return result;} @Overridepublic int sub (int a,int b) {System.out.println ("the Method Sub.....begin"); int result = A-A; System.out.println ("The Method Sub.....end"); return result;} @Overridepublic int mul (int a,int b) {System.out.println ("the Method Mul.....begin"); int result = A * b; System.out.println ("The Method Mul.....end"); return result;} @Overridepublic int div (int a,int b) {System.out.println ("the Method Div.....begin"); int result = A/b; System.out.println ("The Method Div.....end"); return result;}}
There are two kinds of problems with this writing.
(1) Code confusion
As more and more non-business requirements are added, the original business method expands sharply. Each method must handle the core logic while also taking into account several other concerns.
(2) Code dispersion
Take the log requirements as an example, just to meet this single requirement, you have to repeat the same code log in multiple modules, and if the log requirements change, you have to modify all the requirements.
Use dynamic Agent to solve the above problems
Principle: Use a proxy to wrap the object, then replace the original object with the 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.
Arithmeticcalculateproxy.java
public class arithmeticcalculateproxy{//The object to be proxied private arithmeticcalculate target;public arithmeticcalculateproxy () { }public Arithmeticcalculateproxy (arithmeticcalculate target) {this.target = target;} Public Arithmeticcalculate GetProxy () {arithmeticcalculate proxy = null;//proxy object by which class loader is responsible for loading classloader loader = Target.getclass (). getClassLoader ();//type of proxy object, that is, what methods class[] interfaces = new class[]{arithmeticcalculate.class};// When invoking a proxy object where the method is executed, the code invocationhandler handler = new Invocationhandler () {/* * Proxy: The proxy object being returned, typically not used in the Invoke method * Method: Methods being invoked * args: parameters passed in when calling method */@Overridepublic object Invoke (Object Proxy,method method,object[] args) throws Throw Able{string methodName = Method.getname ();//Log System.out.println ("the method" + MethodName + "begin ...");// Execution Method Object result = Method.invoke (Target,args);//Log System.out.println ("the method" + MethodName + "end ..."); return result;}; Proxy = (arithmeticcalculate) proxy.newproxyinstance (Loader,interfaces,handler); return proxy;}}
Test.java
@Testpublic void Testcalculate () {arithmeticcalculate target = new Arithmeticcalculateimpl (); Arithmeticcalculate proxy = new Arithmeticcalculateproxy (target). GetProxy (); System.out.println (Proxy.add (4,2)); System.out.println (Proxy.sub (4,2));}
Results:
The method add Begin ...
The method Add End ...
6
The method sub begin ...
The method Sub End ...
2
Spring Basic Learning (iv)-AOP