Java Dynamic Agent

Source: Internet
Author: User

1. Dynamic Agent Concept

A dynamic agent is a program that automatically creates a proxy object in place of the agent's object to perform the corresponding operation during operation. In the dynamic agent mechanism of Java, there are two important classes or interfaces, one is the Invocationhandler interface and the other is the proxy class, these two are necessary for implementing dynamic Proxy.

2. Implementation of dynamic agents

(1) Create a class to implement the Invocationhandler interface and implement the Invoke method. Each dynamic proxy class must implement the Invocationhandler interface and invoke the method of the proxied real object in the Invoke method and add the corresponding methods and logic to implement the function of the agent before and after it.

(2) Use the Newproxyinstance method of the proxy class to create a proxy object and associate it to a Invocationhandler object

After implementing (1), (2), when we invoke the method of the proxy object, the method call is converted to the Invoke method call of the Invocationhandler object to which the proxy object is associated.

Let's first look at the Invoke method of the Invocationhandler interface

public object invoke (object proxy, Method method, object[] args) throws Throwable {}

This method receives a total of 3 parameters, meaning:

Proxy: Agent Object

Method: Methods object called by the proxy object

Args: Arguments passed in when a proxy object invokes a method

Take a look at the proxy class, the proxy class provides a lot of methods, but the most we use is newproxyinstance this method

public static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler handler) {}

The function of this method is to create a proxy object and associate a Invocationhandler object with the 3 parameters it receives:

Loader: Defines which classloader to load the created proxy object

Interfaces: Indicates that we will provide a set of interfaces to the proxy object, and if we provide a set of interfaces, then this proxy object declares that the interface is implemented (polymorphic) so that we can invoke the methods in this set of interfaces.

Handler: Represents the Invocationhandler object associated with a proxy object

-------------------Talk is cheap,show me the code----------------

Defines an interface in which both real and proxy objects implement the interface

Package com.jiangwangxiang.dynamicProxy;

Public interface IService {
Public Object execute ();
}

Classes of real objects

Package com.jiangwangxiang.dynamicProxy;

public class Majorservice implements iservice{

@Override
Public Object execute () {
System.out.println ("Main business execution ...");
return null;
}
}

Creating a Dynamic proxy class

Package com.jiangwangxiang.dynamicProxy;

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

Create a proxy class to implement the Invocationhandler interface
public class Logproxy implements Invocationhandler {
Real objects by proxy
Private Majorservice target;

Public Logproxy (Majorservice target) {
This.target = target;
}

@Override
public object invoke (object proxy, Method method, object[] args) throws Throwable {
Method logic before method invocation of the real object being proxied
SYSTEM.OUT.PRINTLN ("--------before the execution of the main business");
method to invoke the real object being proxied
Object result = Target.execute ();
Method logic after the method call of the real object being proxied
SYSTEM.OUT.PRINTLN ("----main business post-processing----");
Returns the method return value of the Proxied object, and the generic proxy object only adds some logic before and after the method execution of the Proxied object, without affecting the result of the method execution
return result;
}

}

Test class

Package com.jiangwangxiang.dynamicProxy;

Import Java.lang.reflect.Proxy;

Dynamic Agent Test class
public class Dynamicproxytest {

public static void Main (string[] args) {
Create a real object to be proxied
Majorservice majorservice = new Majorservice ();
Create a Invocationhandler object, passing in the real object of the proxy
Logproxy handler = new Logproxy (majorservice);
Create a proxy object and associate with the Invocationhandler object
IService serviceproxy = (iservice) proxy.newproxyinstance (Handler.getclass (). getClassLoader (),
Majorservice.getclass (). Getinterfaces (), handler);
The method that invokes the proxy object, which will be converted to the Invoke method call of the Invocationhandler object associated with the proxy object
Serviceproxy.execute ();
}

}

-------------------------

The output is:

----The main business pre-processing----
Main business Execution ...
Post-processing--------main business

Java Dynamic Agent

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.