Java development of a pure interface dynamic proxy

Source: Internet
Author: User
Tags throwable

A proxy is a common design pattern designed to provide a proxy for other objects to control access to an object. The proxy class is responsible for preprocessing the message for the delegate class, filtering the message and forwarding the message, and subsequent processing after the message is delegated to the class.

Related classes and interfaces

Java.lang.reflect.Proxy: This is the main class of the Java dynamic agent mechanism, which provides a set of static methods for dynamically generating proxy classes and their objects for a set of interfaces.
Method 1: The method is used to get the calling processor associated with the specified proxy object
Static Invocationhandler Getinvocationhandler (Object proxy)

Method 2: The method is used to get the class object associated with the dynamic proxy class for the specified class loader and a set of interfaces
Static Class Getproxyclass (ClassLoader loader, class[] interfaces)

Method 3: This method is used to determine whether the specified class object is a dynamic proxy class
Static Boolean Isproxyclass (Class cl)

Method 4: The method is used to generate a dynamic proxy class instance for the specified class loader, a set of interfaces, and the calling processor
Static Object newproxyinstance (ClassLoader loader, class[] interfaces,
Invocationhandler h)

Java.lang.reflect.InvocationHandler: This is the call processor interface, which customizes an invoke method that centralizes the method invocation on the dynamic proxy class object, typically implementing proxy access to the delegate class in the method. Each time a dynamic proxy class object is generated, you need to specify a call processor object that implements the interface.
This method is responsible for centralizing all method calls on the dynamic proxy class. The first parameter is both a proxy class instance and the second parameter is the method object that is called
The third method is to call the parameter. The calling processor is preprocessed or dispatched to the delegate class instance on the basis of these three parameters to launch execution
Object Invoke (Object proxy, method, object[] args) Java.lang.ClassLoader: This is the class loader class that is responsible for loading the class's bytecode into Java The virtual machine (JVM) and defines the class object for it before the class can be used. Proxy static method generating dynamic proxy classes also need to be loaded through the class loader to use, Java Development Training It is the only difference from the normal class that its bytecode is generated dynamically by the JVM at runtime rather than pre-existing in any. class file.
Steps
Dynamic agents have the following four steps:
1. Create your own calling processor by implementing the Invocationhandler interface;
2. Create a dynamic proxy class by specifying a ClassLoader object and a set of interface for the proxy class;
3. The constructor of the dynamic proxy class is obtained through the reflection mechanism, and its unique parameter type is the calling processor interface type;
4. Create a dynamic proxy class instance from the constructor by constructing a call to the processor object as a parameter is passed in.

Example

Package twenz.test;

Public interface Userdao {
public void Save (String name);
}

Package twenz.test;

public class Userdaoimp implements Userdao {

@Override
public void Save (String name) {
TODO auto-generated Method Stub
System.out.println (This.getclass (). GetName ());
SYSTEM.OUT.PRINTLN (name);
}
}

Package twenz.test;

Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;

public class ProxyTest2 implements Invocationhandler {
Private Userdao target;

Public ProxyTest2 (Userdao target)
{
This.target = target;
}

    @Override
    public Object Invoke (object proxy, Method method, object[] args) Throws Throwable {
       //TODO auto-generated method stub
        //system.out.println ("The object is:" +proxy.getclass (). GetName ());
        System.out.println ("The method is:" +method.getname ());
        Method.invoke (target, args);
        return null;
   }

public static void test (Userdao target) {
ProxyTest2 proxy = new ProxyTest2 (target);
Userdao user = (Userdao) proxy.newproxyinstance (ProxyTest2.class.getClassLoader (),
Target.getclass (). Getinterfaces (), proxy);
User.save ("=");
}
}

by calling "Proxytest2.test (new Userdaoimp ());" You can trigger an agent that executes the ProxyTest2 invoke function and then triggers the Userdaoimp save function

Proxy for Interface only
This is the focus of this article, that is, if I do not implement the interface, just want to call Userdao's save function? The following code is an example:

Package twenz.test;

Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;

public class Proxytest implements Invocationhandler {
Private class<userdao> target;

Public proxytest (class<userdao> target)
{
This.target = target;
}

@Override
public object invoke (object proxy, Method method, object[] args) throws Throwable {
TODO auto-generated Method Stub
System.out.println ("The object is:" +proxy.getclass (). GetName ());
System.out.println ("The method is:" +method.getname ());
return null;
}

public static void test (class<userdao> target) {
Proxytest proxy = new Proxytest (target);
Userdao user = (Userdao) proxy.newproxyinstance (ProxyTest.class.getClassLoader (),
New Class<?>[]{target}, proxy);
User.save ("");
}

public static void Main (String args[])
{
Proxytest.test (Userdao.class);
}
}

Here we do not implement interface Userdao, but we call the function of the interface save. But here invoke actually does not have the Invoke proxy function save, just obtains the method name of save.

Java development of a pure interface dynamic proxy

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.