Java Architecture Decryption-dual container optimized AOP

Source: Internet
Author: User

In the previous blog, it was proposed that optimization is an endless process, indeed, with the change in demand, the software and hardware base upgrade, we are more and more not consider the capacity of the Code, but consider the quality of the code, but with the depth of research, to a certain stage, we also have to consider the capacity of the code problem, then, the concept of In the previous blog, the service class as an interface passed in, actually in the background is a map container, we not only include the entire implementation of the map, but also the implementation of the service class overlay, but the drawback is that our business class, or a single object, such as:


Class Diagram Transformation

At this point, if we want to provide services for more business, we must write more AOP, which is not in line with our principles, based on this, I made the following adjustments, we first look at the class diagram:

1.1, review the previous version of the class diagram:



1.2, after the transformation of the class diagram:



Code implementation:

We make the business Class A container, and then the proxy class holds the container, and all the child elements of the container, through traversal, have all the methods of the service class:

2.1, Business container:

<pre name= "code" class= "java" ><span style= "font-size:18px;" >/** * Business Class Container: * Business class to cut into service with map * * @author Xu Yu * @version 3.0.0, June 16, 2015 PM 4:41:28 */public class Domehds  imple ments Idomehds {//mapprivate  hashmap<string,object> dobeans;//get business mappublic hashmap<string to execute business Object> Getdobeans () {return dobeans;} Set up business mappublic void Setdobeans (hashmap<string, object> dobeans) {Dobeans = Dobeans;}        } </span>





2.2, proxy class changes:

<span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "java" ><span style= "font-size:18px;" >/** * Hello Dynamic proxy class, add function to business class * Previous version for JDK proxy implementation * This add execution method before printing to console ' befor ' * This time add execution method after printing to console ' after ' * This version is dglib Agent * For proxy class reasons, The JDK proxy requires that the proxy class must implement an interface, because its underlying implementation is to create a new class that implements the same interface as the proxy class * Replace the original business class with the new business class of the proxy class *cglib proxy is a new class, inherit from the proxy class, replace the original business class with the new proxy class, do not need the interface * Version 5.0 adds the Service assembly container, pulling the service out of the proxy class, and our proxy class becomes a bean *6.0 defines the service container as an interface *7.0 add the business container * @author Xu Yu * @version 3.0.0, June 16, 2015 afternoon 3:20:1 3 */public class Cglibdynamicproxy implements Methodinterceptor {//service class container private Iproxymehds proxymehds;//Business class container Priva  Te Domehds Domehds;       Agent Factory class: Singleton mode, optimized memory overhead private static cglibdynamicproxy instance = new Cglibdynamicproxy ();          Constructor Private Cglibdynamicproxy () {}//Get Cglib Agent factory class public static Cglibdynamicproxy getinstance () {      return instance; }/** * uses a proxy factory to generate a proxy for a class * * @param CLS to proxy class * @return return a class that has already been proxied */@SuppressWarnings ("uncheck Ed ") Public &LT      T> T GetProxy (class<t> cls) {return (T) enhancer.create (CLS, this); }//Get business container private void Getproxytodomap () {//Get business map hashmap<string,object> mapdo = Domehds from the business container        . Getdobeans (); Loop to business class in business map Add proxy for (hashmap.entry<string, object> entry:mapDo.entrySet ()) {//Get object in Map Stri        ng Objectkey = Entry.getkey ();                Object objectvalure = Entry.getvalue ();    Re-update the objects in map: Replace the original business method Mapdo.put (Objectkey, GetProxy (Objectvalure.getclass ())); }}//Override the method execution of the Proxied object//All method execution, to the level of reflection is invoke, override this method, rewrite all method execution, implement the proxy @Override public Object inte    Rcept (Object target, method, object[] args, Methodproxy proxy) throws Throwable {//Important improvement: Execute method from service container, no longer write dead!         Proxymehds.beforebean ();                Method normal execution of the statement Object result = Proxy.invokesuper (target, args);        Important improvement: Execute the method from the service container, no longer write dead!                         Proxymehds.afterbean ();  return result;     }//The Get method of the service container public Iproxymehds Getproxymehds () {return PROXYMEHDS;} The set method of the service container public void Setproxymehds (Iproxymehds proxymehds) {This.proxymehds = Proxymehds;} Business container Get method public Domehds Getdomehds () {return DOMEHDS;}           Business Container Set method public void Setdomehds (Domehds domehds) {This.domehds = Domehds;getproxytodomap ();} }</span>


Summary:

Anything, there is a birth and development of the process, human beings so far invented things, are from the human life, in the process of learning, imagine life, we sometimes, will be enlightened, for example:

Xiao Ming to have a courier to Xiao Li Mail past, Xiao Ming find the Postman, signed, after 3 days Xiao Li received, the process of post is like our AOP general, for users is transparent, unpacking and boxing process, to their own responsibility is good, this is not object-oriented!

Of course, is optimization so perfect? Of course no, next blog, we continue to do in-depth understanding and optimization of AOP!


Java Architecture Decryption-dual container optimized AOP

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.