Java Dynamic Proxy 1-use of dynamic Proxy

Source: Internet
Author: User

1. What is dynamic proxy?

A: The dynamic proxy can provide access to another object and hide the actual fact of the object. The proxy generally implements the interface of the actual object it represents. The proxy can access the actual object, but delays the implementation of some functions of the actual object. The actual object implements the actual functions of the system, and the proxy object hides the actual object from the customer. The customer does not know whether it is dealing with agents or actual objects.
2. Why dynamic proxy?

A: The dynamic proxy can process the request.

3. What are the benefits of using it?

A: The dynamic proxy can process the request.
4. Where do I need dynamic proxy?

A: direct access to certain classes is not allowed. Special processing is required for access.

 

Currently, the Java development package includes support for dynamic proxies, but its implementation only supportsInterface. Its implementation mainly uses the java. lang. reflect. Proxy class and java. lang. reflect. InvocationHandler interface.

The Proxy class is mainly used to obtain dynamic Proxy objects, and the InvocationHandler interface is used to constrain the implementation of callers.

The following is a simulated case. The dynamic proxy is used to output two strings to the console before and after the method call.

Directory structure

<Br/>

Define a HelloWorld Interface

1 package com. ljq. test;
2
3 /**
4 * define a HelloWorld Interface
5 *
6 * @ author jiqinlin
7 *
8 */
9 public interface HelloWorld {
10 public void sayHelloWorld ();
11}

<Br/>

Class HelloWorldImpl is the implementation of the HelloWorld Interface

1 package com. ljq. test;
2
3 /**
4 * class HelloWorldImpl is the implementation of the HelloWorld Interface
5 *
6 * @ author jiqinlin
7 *
8 */
9 public class HelloWorldImpl implements HelloWorld {
10
11 public void sayHelloWorld (){
12 System. out. println ("HelloWorld! ");
13}
14
15}

HelloWorldHandler is implemented by the InvocationHandler Interface

1 package com. ljq. test;
2
3 import java. lang. reflect. InvocationHandler;
4 import java. lang. reflect. Method;
5
6 /**
7 * Outputs two strings to the console before and after a method call
8 *
9 * @ author jiqinlin
10 *
11 */
12 public class HelloWorldHandler implements InvocationHandler {
13 // original object to be proxy
14 private Object obj;
15
16 public HelloWorldHandler (Object obj ){
17 super ();
18 this. obj = obj;
19}
20
21 /**
22 * process the method call on the proxy instance and return the result
23 *
24 * @ param proxy
25 * @ param method the proxy method
26 * @ param args parameter array of this method
27 */
28 public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {
29 Object result = null;
30 // before calling
31 doBefore ();
32 // call the method of the original object
33 result = method. invoke (obj, args );
34 // after the call
35 doAfter ();
36 return result;
37}
38
39 private void doBefore (){
40 System. out. println ("before method invoke ");
41}
42
43 private void doAfter (){
44 System. out. println ("after method invoke ");
45}
46
47}

Test class

Package com. ljq. test;

Import java. lang. reflect. InvocationHandler;
Import java. lang. reflect. Proxy;


Public class HelloWorldTest {

Public static void main (String [] args ){
HelloWorld helloWorld = new HelloWorldImpl ();
InvocationHandler handler = new HelloWorldHandler (helloWorld );

// Create a dynamic proxy object
HelloWorld proxy = (HelloWorld) Proxy. newProxyInstance (
HelloWorld. getClass (). getClassLoader (),
HelloWorld. getClass (). getInterfaces (),
Handler );
Proxy. sayHelloWorld ();
}
}

The running result is:

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.