Java architecture decryption-real-time dynamic aop

Source: Internet
Author: User

Java architecture decryption-real-time dynamic aop

In the previous blog, we set up a container that encapsulates the business, so that we can assemble the business and services to the client, the client is equivalent to the configuration file used in our development. Have you found any problems? Why can't I change it dynamically? The business container is not allowed to add a new container after the proxy is complete. The container is the container that works during compilation, and thus it loses a lot of flexibility! What should we do? Actually, it is better to change the call sequence. Let's take a look at the results:
1. Comparison of class charts

Class Diagram after transformation

2. Code implementation: 2.1 proxy class changes:
/*** Greeting dynamic proxy class, add a function to the service class * the previous version implements the JDK proxy * print the execution method to the console 'befor 'before this addition * print the execution method to the console after this addition * the current version is DGLIB proxy * for proxy reasons, JDK proxy requires that the proxy class must implement an interface, because its underlying implementation is to create a new class, implement the same interface as the proxy class * use the service class created by the proxy class to replace the original service class * CGLIB proxy to create a new class that inherits from the proxy class and replaces the original service class with the newly created proxy class, no interface is required. ** in version 5.0, services are added to assemble containers and services are removed from the proxy class, our proxy class becomes a bean * 6.0 defines the service container as an interface * 7.0 adds the business container * 8.0 the business container is not flexible, and places the obtained proxy to the business container, to dynamically add elements, you can also get the proxy * @ author Xu Shu * @ version 3.0.0, June 16, 2015 3:20:13 */public class CGLibDynamicProxy implements MethodInterceptor {// service class container private IProxyMehds proxyMehds; // proxy factory class: Singleton mode, optimized memory overhead private static CGLibDynamicProxy instance = new CGLibDynamicProxy (); // constructor private CGLibDynamicProxy () {}// obtain the cglib proxy factory class public static CGLibDynamicProxy getInstance () {return instance ;} /*** use the proxy factory to generate the proxy for a class ** @ param cls the class to be proxy * @ return returns the class that has already been proxy */@ SuppressWarnings (unchecked) public
  
   
T getProxy (Class
   
    
Cls) {return (T) Enhancer. create (cls, this) ;}// rewrite the method execution of the proxy object // all the method execution, to the reflection level is invoke, override this method, all Method executions are rewritten, and proxy @ Override public Object intercept (Object target, method Method, Object [] args, MethodProxy proxy) throws Throwable {// important improvements: the execution method from the service container is no longer written to death! ProxyMehds. beforeBean (); // The statement Object result = proxy. invokeSuper (target, args); // important improvement: the execution method from the service container is no longer writable! ProxyMehds. afterBean (); return result;} // get method of the Service container public IProxyMehds getProxyMehds () {return proxyMehds;} // set Method of the Service container public void setProxyMehds (IProxyMehds proxyMehds) {this. proxyMehds = proxyMehds ;}}
   
  
2.2 Business containers
/*** Business containers: * use map to store the business classes to be switched into the service ** @ author Xu Shu * @ version 3.0.0, 14:25:21 */public class DoMehds implements IDoMehds {// map private HashMap for executing services
  
   
DoBeans; // proxy class private CGLibDynamicProxy;/*** encapsulate the method for obtaining elements, each element retrieved encapsulates a proxy ** @ param element name * @ return proxy element */public Object getBean (String beanName) {return proxy. getProxy (DoBeans. get (beanName ). getClass ();} // obtain the Business map public HashMap
   
    
GetDoBeans () {return DoBeans;} // sets the Business map public void setDoBeans (HashMap
    
     
DoBeans) {DoBeans = doBeans;} // obtain the proxy class public CGLibDynamicProxy getProxy () {return proxy;} // set the proxy class public void setProxy (CGLibDynamicProxy proxy) {this. proxy = proxy ;}}
    
   
  
2.3 Client
/*** The Client that executes the agent ** @ author Xu Shu * @ version 3.0.0, 3:18:42, January 1, June 16, 2015 */public class Client {// The main method that the Client executes: public static void main (String [] args) {// container HashMap of the object before the method is executed
  
   
BeforBeans; HashMap
   
    
AfterBeans; // method HashMap to be executed before the preparation method is executed
    
     
BeforMethods; HashMap
     
      
AfterMethods; // The service class map beforMethods = new HashMap (); beforBeans = new HashMap (); afterMethods = new HashMap (); afterBeans = new HashMap (); // Add the service class AspectClass1 to the service class beforBeans to be loaded during method execution. put (AspectClass1, new AspectClass1 (); beforBeans. put (AspectClass2, new AspectClass2 (); afterBeans. put (AspectClass3, new AspectClass3 (); afterBeans. put (AspectClass4, new AspectClass4 (); // specifies the method beforMethods to be executed for each service class. put (AspectClass1, SayHello); beforMethods. put (AspectClass2, SayGoodBye); afterMethods. put (AspectClass3, SayHi); afterMethods. put (AspectClass4, Eat); // an instance service container that imports four maps into the container: ProxyMehds proxyMehds = new ProxyMehds (); proxyMehds. setBeforBeans (beforBeans); proxyMehds. setBeforMethods (beforMethods); proxyMehds. setAfterBeans (afterBeans); proxyMehds. setAfterMethods (afterMethods); // map HashMap of the Instance business
      
        DoBeansMap = new HashMap
       
         (); // Load the business to DoBeansMap in map. put (dobeans1, new GreetingImpl (); DoBeansMap. put (dobeans2, new EatClass (); // load the Business map to the container DoMehds doMehds = new DoMehds (); doMehds. setDoBeans (DoBeansMap); // instance proxy class CGLibDynamicProxy cglib = CGLibDynamicProxy. getInstance (); // accept cglib. setProxyMehds (proxyMehds); doMehds. setProxy (cglib); // accept the Greeting greeting = (Greeting) doMehds object to be proxy. getBean (dobeans1); EatClass eatObject = (EatClass) doMehds. getBean (dobeans2); // method of the execution object greeting. sayHello (Jack); eatObject. eat ();}}
       
      
     
    
   
  
3. Summary
Code is flexible, just like life. In the process of code flexibility, we will compare life and life, and each of us, things, and things are independent objects, in the timeline of our life, we are also a procedural organization. Our code and life are the same in our thoughts. The optimization is not only code, but also ideas!

 

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.