Proxy mode (Add dynamic proxy)

Source: Internet
Author: User
Proxy mode: provides a proxy for other objects to control access to this object. The following is an example:

Public class objectimpl implements object {

Public void action () {system. out. println ("==========="); system. out. println ("==========="); system. out. println ("this is the proxy class"); system. out. println ("==========="); system. out. println ("========== ");}}

 

Public class proxyobject {object; Public proxyobject () {system. out. println ("this is a proxy class"); object = new objectimpl ();} public void action () {system. out. println ("proxy start"); object. action (); system. out. println ("proxy ended ");}

}

 

Public class test {

Public static void main (string [] ARGs ){

Proxyobject OBJ = new proxyobject (); obj. Action ();}}

 

Dynamic Proxy:

By observing the code, we can find that each proxy class can only serve one interface. In this way, too many Proxies are generated in program development. Besides the called methods, all proxy operations are different, if all other operations are the same, the Code must be repeated. The best way to solve this problem is to use a dynamic proxy to complete all proxy functions through one proxy class. Let's take a look at the dynamic Proxy: JDK dynamic proxy contains a class and an interface: invocationhandler interface: public interface invocationhandler {public object invoke (Object proxy, method, object [] ARGs) throws throwable;} parameter description: Object Proxy: refers to the object to be proxy. Method method: Method object to be called [] ARGs: The parameter required for method calling can think of the subclass of the invocationhandler interface as the final operation class of a proxy and replace proxysubject. Proxy class: the proxy class is an operation class dedicated to proxy. You can use this class to dynamically generate an implementation class for one or more interfaces. This class provides the following operation methods: public static object newproxyinstance (classloader loader, class <?> [] Interfaces, invocationhandler h) throws illegalargumentexception parameter description: classloader Loader: Class Loader <?> [] Interfaces: obtain all interface invocationhandler H: Obtain the subclass instance of the invocationhandler interface PS: The Class Loader needs a classloader class instance in the newproxyinstance () method of the proxy class, classloader actually corresponds to a Class Loader. in Java, There are mainly three types of loaders. boost?classloader: This loader is written in C ++, which is not seen in development; extendsion classloader: used to load extension classes. Generally, it corresponds to classes in the JRE \ Lib \ ext directory. appclassloader: (default) It loads the class specified by classpath, is the most commonly used is a loader. Dynamic proxy is compared with static proxy. The Byte Code of the dynamic proxy class is dynamically generated by the Java reflection mechanism when the program is running, without the need for programmers to manually write its source code. Dynamic proxy not only simplifies programming, but also improves the scalability of the software system, because the Java reflection mechanism can generate any type of dynamic proxy class. The proxy class and invocationhandler interface in the Java. Lang. Reflect package provide the ability to generate dynamic proxy classes. Dynamic proxy example: 1. bookfacade. java code package net. battier. dao; public interface bookfacade {public void addbook ();} 2. bookfacadeimpl. java code package net. battier. dao. impl; import net. battier. dao. bookfacade; public class bookfacadeimpl implements bookfacade {@ override public void addbook () {system. out. println ("add the library method... ") ;}}, Bookfacadeproxy. java package net. battier. proxy; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy;/*** JDK dynamic proxy class ** @ author student **/public class bookfacadeproxy implements invocationhandler {private object target; /*** bind the delegate object and return a proxy class * @ Param target * @ return */public object BIND (object target) {this.tar get = target; // obtain the proxy object return proxy. newproxyinstance (target. getclass (). getclassloader (), target. getclass (). getinterfaces (), this); // interface to be bound (this is a defect, cglib makes up for this defect )} @ override/*** call Method */public object invoke (Object proxy, method, object [] ARGs) throws throwable {object result = NULL; system. out. println ("transaction start"); // execution method result = method. invoke (target, argS); system. out. println ("transaction end"); return result ;}} 3. testproxy. java code package net. battier. test; import net. battier. dao. bookfacade; import net. battier. dao. impl. bookfacadeimpl; import net. battier. proxy. bookfacadeproxy; public class testproxy {public static void main (string [] ARGs) {bookfacadeproxy proxy = new bookfacadeproxy (); bookfacade bookproxy = (bookfacade) proxy. BIND (New bookfacadeimpl (); bookproxy. addbook ();}}

 

 

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.