Easy to master Java Proxy mode _java

Source: Internet
Author: User

Have a chat with everyone. Java proxy mode

1. Static Agent

1.1 The proxy class of the static proxy and the class being represented should maintain a common interface.

Public interface Iuserdao {
  void Save ();

1.2 Proxy classes, target objects

The public class Userdao implements
  iuserdao{
  is @Override public void Save () {
    System.out.println ("-----has saved the data!!! ------");
  }
} 


1.3 Proxy objects

public class Userdaoproxy implements iuserdao{
  //Receive Save target object
  private Iuserdao target;
  Public Userdaoproxy (Iuserdao target) {
    this.target = target;
  }
  @Override public
  Void Save () {
    System.out.println ("Start transaction ...");
    Target.save ();       The method to execute the target object
    System.out.println ("Commit transaction ...");
  }


1.4 Test Class

public class App. {public
  static void Main (string[] args) {
    //target object
    Iuserdao target = new Userdao ();
    Agent
    Iuserdao proxy = new Userdaoproxy (target);
    Proxy.save (); Execution is, agent's method
  }
}

2. Dynamic Agent

2.1 Similarly, dynamic proxies also need to complete an interface. Ibid.

2.2 The target object is the same.

2.3 is only different on the proxy object

public class Proxyfactory {
  //maintains a target object
  private object target;
  Public proxyfactory (Object target) {
    this.target = target;
  }
  To the target object, generate the Proxy object public object 
  getproxyinstance () {return proxy.newproxyinstance () (
        target.getclass). getClassLoader (), 
        Target.getclass (). Getinterfaces (),
        new Invocationhandler () {
          @Override
          public object invoke (object proxy, Method method, object[] args)
              throws Throwable {
            System.out.println ("Open Transaction" );
            Execute target object Method object
            returnvalue = Method.invoke (target, args);  
            SYSTEM.OUT.PRINTLN ("Submit Transaction");
            Return returnvalue
          }}
        );}


2.4 Test class

public class App. {public
  static void Main (string[] args) {
    //target object
    Iuserdao target = new Userdao ();
    System.out.println (Target.getclass ());
    To the target object, create the proxy object
    Iuserdao proxy = (Iuserdao) new Proxyfactory (target). Getproxyinstance ();
    System.out.println (Proxy.getclass ());
    Execution method  "proxy object"
    proxy.save ();
  }


3, Cglib Agent

3.1cglib agent does not need to complete the interface, only to write the class and proxy class can be proxy, where the proxy is similar to 1.2, so no longer write.

3.2 Proxy classes are different, and using the Cglib proxy mode requires referencing spring's core framework package.

public class Proxyfactory implements methodinterceptor{
  //maintains target object
  private object target;
  Public proxyfactory (Object target) {
    this.target = target;
  }
  Creates a proxy object for the target object
  getproxyinstance () {
    //1. Tool class
    enhancer en = new enhancer ();
    2. Sets the parent class
    En.setsuperclass (Target.getclass ());
    3. Set the callback function
    En.setcallback (this);
    4. Create subclass (proxy object) return
    en.create ();
  }
  @Override Public
  Object Intercept (Object obj, Method method, object[] args,
      Methodproxy proxy) throws Throwable {
    System.out.println ("Start transaction ...");
    Method of executing target object
    returnvalue = Method.invoke (target, args);
    SYSTEM.OUT.PRINTLN ("Commit transaction ...");
    Return returnvalue
  }
}

3.3 Test class

public class App. {public
  static void Main (string[] args) {
    //target object
    Userdao target = new Userdao ();
    System.out.println (Target.getclass ());
    Proxy object
    Userdao proxy = (Userdao) new Proxyfactory (target). Getproxyinstance ();
    System.out.println (Proxy.getclass ());
    The method to execute the proxy object
    proxy.save ();
  }


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.