Java Proxy Mode

Source: Internet
Author: User

The concept of proxy mode: Provides a proxy for other objects to control access to this object.

Application scenario: In the development of some business class methods, in addition to the requirements of operations, but also need to do other non-business operations. such as the interface provided to the app. In addition to the business logic to be implemented by the interface, the information of the user, the information of the device, and the encryption and decryption of the parameters are also verified. This is called before each interface method, and the business code is duplicated and cumbersome when it is joined together. These frequently used methods can be extracted from the proxy class. The business class only takes care of business operations and reduces coupling.

Static proxy

The agent and the Proxied object are determined before the agent. They all implement the same interface or inherit the same abstract class.

Dynamic Agent

Dynamically generate proxies for different classes, different methods of proxy

1) JDK Dynamic agent

Java provides dynamic proxy technology that allows developers to create proxy instances of the interface at run time. The JDK dynamic agent mainly involves the proxy and Invocationhandler under the Java.lang.reflect package.

Interface

 Public Interface traffictools{        publicvoid Drive ();

Realize

 Public class Implements traffictools {    @Override    publicvoid Drive () {        System.out.println ("car driving ... ");    }}

proxy class

ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classCarproxyImplementsinvocationhandler{ PublicCarproxy (Object target) {Super();  This. target =Target; }    PrivateObject Target; @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {LongStart =System.currenttimemillis (); System.out.println ("The car began to drive ... ");       Method.invoke (target); LongEnd =System.currenttimemillis (); System.out.println ("The car ended up driving ... "+ (End-start)); return NULL; }}

Test agent

Import Java.lang.reflect.Proxy;  Public class Test {        publicstaticvoid  main (string[] args) {        new Car ();         New carproxy (car);        Class<?> cls = car.getclass ();         = (Traffictools) proxy.newproxyinstance (Cls.getclassloader (),                  cls.getinterfaces (), h);        T.drive ();    }}

2) cglib Dynamic Agent

The JDK dynamic agent can only create dynamic proxies for interfaces, and Cglib uses very low-level bytecode technology to create proxies for a class. A jar package that references cglib is required.

ImportJava.lang.reflect.Method;ImportNet.sf.cglib.proxy.Enhancer;ImportNet.sf.cglib.proxy.MethodInterceptor;ImportNet.sf.cglib.proxy.MethodProxy; Public classCglibproxyImplementsmethodinterceptor{PrivateEnhancer enhancer =Newenhancer ();  PublicObject getproxy (Class clazz) {enhancer.setsuperclass (clazz); Enhancer.setcallback ( This); returnenhancer.create (); }    /*** * Intercept method calls for all target classes*/@Override PublicObject Intercept (Object obj, Method m, object[] args, methodproxy proxy)throwsThrowable {returnproxy.invokesuper (obj, args); }}

The performance of dynamic proxy objects created by Cglib is much higher than the performance of the proxy objects created by the JDK. However, Cglib takes more time to create a proxy object than the JDK dynamic agent.

Java Proxy Mode

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.