Java Dynamic Agent

Source: Internet
Author: User

One, static agent and dynamic proxy comparison:

1, static agent writing requirements:

1) The decorator and decorator need to implement the same interface or implement the same class.

2) The decorator should have a reference to the decorator.

3) Strengthened methods are needed.

4) Do not need to strengthen the method, the implementation of the original method.

2. Dynamic Agent Method

When the project is running, generate an object for the agent to enhance the method.

2 different ways:

1) The method proxy class in the JDK, provided that the interface is implemented.

2) Spring in Cglib, premise: Inherit class.

Dynamically creates a proxy object in memory.

One of the implementations in the JDK:

Static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h)

Parameter description:

1) ClassLoader Loader: Is the class loader that the proxy object loads the memory. Typically, the class loader for the Proxied object is used.

2) class<?>[] Interfaces: A collection of interfaces that a proxy object needs to implement, typically a collection of interfaces implemented by the Proxied object. 、

3) Invocationhandler: the implementation of the processing class, here on the need to strengthen the method, to strengthen.

Invocationhandler:interface Invocationhandler. He is an interface, and we can implement that interface through anonymous classes.

Method: Object Invoke (Object proxy, Method method, object[] args)

Parameter description:

Proxy: Agent object.

Method: The currently executing methods.

Object[] args: The parameters required for the currently executing method.

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.