Java-12.5 proxy and dynamic proxy

Source: Internet
Author: User

Java-12.5 proxy and dynamic proxy

This section describes proxy and dynamic proxy.

1. Why do I need a proxy?

Answer: You don't want to integrate some operations into the logic code, so you need a proxy to do this. For example, log, monitoring, etc.

The following shows a simple Proxy:

 

package com.ray.ch11;public class Test {public static void test(InterFace interFace) {interFace.doSomething();interFace.doElsething();}public static void main(String[] args) {test(new RealObject());test(new SimpleProxy(new RealObject()));}}interface InterFace {void doSomething();void doElsething();}class RealObject implements InterFace {@Overridepublic void doSomething() {System.out.println(RealObject doSomething);}@Overridepublic void doElsething() {System.out.println(RealObject doElsething);}}class SimpleProxy implements InterFace {private InterFace interFace;public SimpleProxy(InterFace interFace) {this.interFace = interFace;}@Overridepublic void doSomething() {System.out.println(SimpleProxy doSomething);interFace.doSomething();}@Overridepublic void doElsething() {System.out.println(SimpleProxy doElsething);interFace.doElsething();}}

Output:

 

RealObject doSomething
RealObject doElsething
SimpleProxy doSomething
RealObject doSomething
SimpleProxy doElsething
RealObject doElsething

From the code above, we can see that only the real object and the proxy need to implement an interface at the same time to meet the basic requirements for implementing the proxy. In fact, the above Code outputs two sentences in the proxy, we don't want to integrate it into the logic code, so we need to proxy this class to deal with these things.

 

2. Dynamic proxy

In java, not only the proxy mode exists, but also the agent can be dynamically created.

Let's look at the following code. We replaced our static proxy with the dynamic proxy of java.

 

package com.ray.ch11;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class Test {public static void test(InterFace interFace) {interFace.doSomething();interFace.doElsething();}public static void main(String[] args) {InterFace proxy = (InterFace) Proxy.newProxyInstance(InterFace.class.getClassLoader(), new Class[] { InterFace.class },new DynamicProxyHandler(new RealObject()));test(proxy);}}class DynamicProxyHandler implements InvocationHandler {private InterFace interFace;public DynamicProxyHandler(InterFace interFace) {this.interFace = interFace;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {System.out.println(proxy dosomething);System.out.println(proxy.getClass());System.out.println(method);return method.invoke(interFace, args);}}interface InterFace {void doSomething();void doElsething();}class RealObject implements InterFace {@Overridepublic void doSomething() {System.out.println(RealObject doSomething);}@Overridepublic void doElsething() {System.out.println(RealObject doElsething);}}

Output:

 

Dosomething
Class com. ray. ch11. $ Proxy0
Public abstract void com. ray. ch11.InterFace. doSomething ()
RealObject doSomething
Dosomething
Class com. ray. ch11. $ Proxy0
Public abstract void com. ray. ch11.InterFace. doElsething ()
RealObject doElsething

 

The code above implements the functions mentioned at the first point, but there are some parameters in Proxy. newProxyInstance that need to be noted.

1. It is classLoader. This loader is generally a loader that has loaded objects. The loader above can be either an InterFace loader or a Test loader.

2. List of interfaces you want to implement (this must be an interface, not a class or abstract class)

3. InvocationHandler implementation

4. In the implementation of DynamicProxyHandler, proxy is actually a proxy, not an actual class.

5. Use the method. invoke (interFace, args) method to execute the real class method

6. We can even control the execution method through the method name.

 

Summary: This chapter describes proxy and 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.