In the previous blog, we introduced the basic implementation of AOP in the macro, see the blog Java architecture to decrypt the--spring framework of AOP, in the blog, we describe some of the AOP design ideas, today, we on the concrete implementation, and we discuss together, Look at how AOP is coming to today, and there are some diagrams that need to be corrected!
One, Code evolutionpremise: The first function to be implemented (hello)
Code:
Interface:
<span style= "FONT-SIZE:18PX;" >public interface Greeting {void SayHello (String name); } </span>
implementation:
<span style= "FONT-SIZE:18PX;" >public class Greetingimpl implements greeting {@Override public void SayHello (String name) { System.out.println ("hello!" + name); } } </span>
1, to add a function before greeting to join print "befor", after greeting to print "after"
Modified implementation:
<span style= "FONT-SIZE:18PX;" >public class Greetingimpl implements greeting {@Override public void SayHello (String name) { before (); System.out.println ("hello!" + name); After (); } private void Before () { System.out.println ("before"); } private void After () { System.out.println (' after '); } } </span>
2, I want to manage theseif one day, the dark code snippet in Figure 1 needs to be modified, is it necessary to open the 3-place code to modify it? What would happen if the code was not included in 3 places, but in 100 places or even 1000 places?
To solve this problem, we typically define the dark code portion of 1 as a method, and then call the method separately in 3 code snippets.
The implementation of the agent:
<span style= "FONT-SIZE:18PX;" >public class Greetingproxy implements greeting {private Greetingimpl Greetingimpl; Public Greetingproxy (Greetingimpl greetingimpl) { This.greetingimpl = Greetingimpl; } @Override public void SayHello (String name) { before (); Greetingimpl.sayhello (name); After (); } private void Before () { System.out.println ("before"); } private void After () { System.out.println (' after '); } } </span>
Note: At this time, there is no befor and after method in Greetingimpl!
3, dynamic replacement agentBecause the software system requirements change is very frequent things, the system pre-design Method 1, Method 2, Method 3 only realize the core business functions, after a period of time, we need to Method 1, Method 2, Method 3 to increase transaction control; After a while, the customer proposed Method 1, Method 2, Method 3 need user legitimacy Verify that only legitimate users can execute these methods; After a period of time, the customer also proposed method 1, Method 2, Method 3 should increase the logging; another period of time, the customer also proposed ... In the face of such a situation, what do we do?
Class Design:
JDK Proxy implementations:
<span style= "FONT-SIZE:18PX;" >public class Jdkdynamicproxy implements Invocationhandler { private Object target; Public Jdkdynamicproxy (Object target) { this.target = target; } @SuppressWarnings ("unchecked") public <T> T getproxy () { return (t) proxy.newproxyinstance ( Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), this ); } @Override Public object Invoke (Object proxy, Method method, object[] args) throws Throwable { before (); Object result = Method.invoke (target, args); After (); return result; } private void Before () { System.out.println ("before"); } private void After () { System.out.println (' after '); } } </span>
Client:
<span style= "FONT-SIZE:18PX;" >public class Client {public static void main (string[] args) { greeting greeting = new Jdkdynamicproxy (New greeting Impl ()). GetProxy (); Greeting.sayhello ("Jack"); } } </span>
Relationship of run-time objects:
4.1 Improvements
Cons: Must have an interface
Cglib Agent:
Cglib implementation:
<span style= "FONT-SIZE:18PX;" >public class Cglibdynamicproxy implements Methodinterceptor { private static cglibdynamicproxy instance = new CGL Ibdynamicproxy (); Private Cglibdynamicproxy () { } public static Cglibdynamicproxy getinstance () { return instance; } @SuppressWarnings ("unchecked") public <T> T getproxy (class<t> cls) { return (T) enhancer.create ( CLS, this); } @Override Public Object Intercept (object target, Method method, object[] args, Methodproxy proxy) throws Throwable { C12/>before (); Object result = Proxy.invokesuper (target, args); After (); return result; } private void Before () { System.out.println ("before"); } private void After () { System.out.println (' after '); } } </span>
Client:
<span style= "FONT-SIZE:18PX;" >public class Client {public static void main (string[] args) { Greeting greeting = cglibdynamicproxy.getinstance () . GetProxy (Greetingimpl.class); Greeting.sayhello ("Jack"); }</span>
Relationship of run-time objects:
Summary:
To get here, the general blog may end, because the core content is over, but our journey is the Stars and the sea, our journey is just beginning, this AOP is very hasty, has not yet implemented dynamic assembly, and our AOP facets (public service) and proxy classes are written together, and so on these issues, To solve these problems, we need to stand on the whole design ideas, the core principle of understanding, the remaining abstraction and encapsulation of the work, is the test of a person another ability of the time, we, the next section continue!
Java Architecture Decryption--AOP dynamic Proxy implementation