Java architecture decryption-in-depth reconstruction of AOP
In my previous blog, I have studied the basic implementation of AOP, but I have also left you with many questions. In this blog, let's work together to explore how to continuously optimize these problems and look at what kind of thing AOP will grow into in our hands!
Review:
Let's take a look at the AOP class diagram we implemented in the previous blog:
Let's look at the problems in the CGLIB class.
public class CGLibDynamicProxy implements MethodInterceptor { private static CGLibDynamicProxy instance = new CGLibDynamicProxy(); private CGLibDynamicProxy() { } public static CGLibDynamicProxy getInstance() { return instance; } @SuppressWarnings(unchecked) public
T getProxy(Class
cls) { return (T) Enhancer.create(cls, this); } @Override public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable { before(); Object result = proxy.invokeSuper(target, args); after(); return result; } private void before() { System.out.println(Before); } private void after() { System.out.println(After); } }
1. extended services
In this way, our expansion will become difficult. Let's think about this scenario. To add a function to the business we write, we need to write a proxy class, function changes, and code changes, this puts a lot of burden on our code maintenance! If we want to expand two methods for the first time, write the proxy, expand three for the next time, write the proxy, and then expand one for the next time write the proxy, what should I do if I don't want the first 2nd methods? Indeed, we do not want to see such maintenance. Therefore, we put the extended public service in a container. You can see the class diagram:
1.1 encapsulate public services:
Public class ProxyMehds {// container private HashMap of the object before the method is executed
BeforBeans; private HashMap
AfterBeans; // methods to be executed before execution of the preparation method private HashMap
BeforMethods; private HashMap
AfterMethods; @ Override public void beforeBean () {try {for (HashMap. Entry
Entry: beforBeans. entrySet () {String objectKey = entry. getKey (); Object objectValure = entry. getValue (); Method beforMehod = objectValure. getClass (). getMethod (beforMethods. get (objectKey); beforMehod. invoke (objectValure) ;}} catch (Exception ex) {ex. printStackTrace ();} // Method sAge = c. getMethod (setAge) ;}@ Override public void afterBean () {try {for (HashMap. entry
Entry: afterBeans. entrySet () {String objectKey = entry. getKey (); Object objectValure = entry. getValue (); Method beforMehod = objectValure. getClass (). getMethod (afterMethods. get (objectKey); beforMehod. invoke (objectValure) ;}} catch (Exception ex) {ex. printStackTrace () ;}} public HashMap
GetBeforBeans () {return beforBeans;} public void setBeforBeans (HashMap
BeforBeans) {this. beforBeans = beforBeans;} public HashMap
GetAfterBeans () {return afterBeans;} public void setAfterBeans (HashMap
AfterBeans) {this. afterBeans = afterBeans;} public HashMap
GetBeforMethods () {return beforMethods;} public void setBeforMethods (HashMap
BeforMethods) {this. beforMethods = beforMethods;} public HashMap
GetAfterMethods () {return afterMethods;} public void setAfterMethods (HashMap
AfterMethods) {this. afterMethods = afterMethods ;}}
1.2 encapsulation proxy class:
public class CGLibDynamicProxy implements MethodInterceptor { private static CGLibDynamicProxy instance = new CGLibDynamicProxy(); private ProxyMehds proxyMehds; private CGLibDynamicProxy() { } public static CGLibDynamicProxy getInstance() { return instance; } @SuppressWarnings(unchecked) public
T getProxy(Class
cls) { return (T) Enhancer.create(cls, this); } @Override public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable { proxyMehds.beforeBean(); Object result = proxy.invokeSuper(target, args); proxyMehds.afterBean(); System.out.println(); return result; }public ProxyMehds getProxyMehds() {return proxyMehds;}public void setProxyMehds(ProxyMehds proxyMehds) {this.proxyMehds = proxyMehds;} }
1.3 service class:
public class AspectClass1 {public void SayHello(){System.out.println(This is AspectClass1.SayHello!);}}
1.4 client:
Public class Client {public static void main (String [] args) {HashMap
BeforBeans; HashMap
AfterBeans; HashMap
BeforMethods; HashMap
AfterMethods; beforMethods = new HashMap (); beforBeans. put (AspectClass1, new AspectClass1 (); beforMethods. put (AspectClass1, SayHello); beforMethods. put (AspectClass2, SayGoodBye); afterMethods = new HashMap (); afterBeans. put (AspectClass3, new AspectClass3 (); afterBeans. put (AspectClass4, new AspectClass4 (); afterMethods. put (AspectClass3, SayHi); afterMethods. put (AspectClass4, Eat); ProxyMehds proxyMehds = new ProxyMehds (); proxyMehds. setBeforBeans (beforBeans); proxyMehds. setBeforMethods (beforMethods); proxyMehds. setAfterBeans (afterBeans); proxyMehds. setAfterMethods (afterMethods); // instance proxy class CGLibDynamicProxy cglib = CGLibDynamicProxy. getInstance (); // accept cglib. setProxyMehds (proxyMehds); // accept the Greeting greeting = cglib. getProxy (GreetingImpl. class); // execute a method greeting of the object. sayHello (Jack );}}
Summary:
What is the benefit of this encapsulation? Our service is no longer fixed, but an empty shell is defined in the proxy. In this way, every time we use it, it is a reusable empty shell, this is also the framework we often call. Later service classes are added and developed in the dynamic initialization of the client. This makes it easy for us to expand and write a class, we can directly configure xml (similar to client Assembly) to implement this function! Makes function expansion easy!
In code implementation, function implementation is the first step in code writing, and most of our programmers have done the first step, in order to get a higher salary, there must be something different from others. At this time, the business will understand the level, abstract ability, implementation ability, and optimization ability, and gradually play a crucial role in future promotions, however, these capabilities have a common parent class, that is, the concept of hierarchy. In terms of global consideration, there are two directions: Time and Space. What should I do if the changes take a long time? What if the number of people is large and the data volume is large?