The understanding of Java Proxy Mode two--dynamic proxy (JDK)

Source: Internet
Author: User
Tags reflection
the understanding of Java Proxy Mode two--dynamic proxy (JDK)

Let's go on to the static agent on the blog to start today's dynamic agent. One, dynamic agent

Static agent needs to be written before the proxy class, which caused a lot of duplication of code, so we through dynamic agents in the runtime dynamic generation of business class proxy class, then the dynamic proxy class is how to achieve it.

The byte code of the dynamic proxy class is generated dynamically by the Java reflection mechanism when the program is run, without the programmer writing its source code manually. Dynamic proxy classes Not only simplify programming, but also improve the scalability of software systems, because the Java reflection mechanism can generate any type of dynamic proxy class. The proxy class and the Invocationhandler interface in the Java.lang.reflect package provide the ability to generate dynamic proxy classes. The original is to use the mechanism of reflection to achieve, today we do not discuss reflection, we see the JDK dynamic Proxy implementation.

JDK Dynamic proxy contains a class and an interface: Invocationhandler interface, and we define a implementation class "Proxy", this is a universal proxy class, we are through this proxy class to dynamically proxy.
Invocationhandler Interface:
Public interface Invocationhandler {
public object Invoke (Object Proxy,method method,object[] args) throws Throwable;
}
Parameter description:
Object proxy: Refers to the objects being represented.
Methods: The method to invoke
object[] args: Parameters required for method invocation

The subclass of the Invocationhandler interface can be imagined as the final action class of an agent, replacing the proxysubject.


      proxy class: &NBSP
      proxy class is an action class that completes the agent specifically. This class enables you to dynamically build an implementation class for one or more interfaces, which provides the following action methods: &NBSP
     public static Object newproxyinstance ( ClassLoader Loader, class<?>[] interfaces, Invocationhandler h)  
                                 throws illegalargumentexception 
      Parameter description:  
           ClassLoader Loader: class loader  
           class<?>[] Interfaces: Get all the interface  
           invocationhandler H: Gets the subclass instance of the Invocationhandler interface  


Ps: Class Loader
The Newproxyinstance () method in the proxy class requires an instance of the ClassLoader class, ClassLoader actually corresponds to the ClassLoader, and there are three kinds of loaders in Java.
Booststrap ClassLoader: This loader is written in C + +, general development is not seen;
Extendsion ClassLoader: Used for extending class loading, generally corresponding to the Jre\lib\ext directory of classes;
Appclassloader: (default) load Classpath specified class, is the most commonly used is a loader.
ii. dynamic proxies using JDK

Or use the code of the previous blog, we are also simple to say three steps to achieve: business interface, business implementation class creation, proxy class creation, business invocation.

   We see Code implementation:   [java]  view plain  copy <span style= "Font-family:simsun; font-size:18px; " ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > /**       *  define a business interface           *  @author  Cassie         */            public interface Account {                //  Query                public void queryAccount  ();                       //  Modification                 public void  updateaccount  ();              }                 /**         *  Interface implementation class (including business logic)           *  : Delegate class         *  @author  cassie           */            public class AccountImpl implements Account{                          @ override               public void  queryaccount ()  {                    system.out.println ("Query method ...");                      }                            @Override                 public void  Updateaccount ()  {                    system.out.println ("Modify Method ...");                      }                       }</span></span>      

The key dynamic agents are the following lines of code: The proxy class Proxy does not implement a specific business interface, but rather implements the Invocationhander class provided by the JDK. In the proxy we do not need to know the specific business class, that is, the delegate class, before running, the delegate class and proxy classes to decouple, in the Run-time only occurs when the connection, is achieved through this sentence: private object target. Then, at the time of the call, pass getinstance to determine who is the delegate object.

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.