Java Dynamic proxy, dynamic proxy

Source: Internet
Author: User
Package dynamicproxy; public interface simpleinterface {void dosomething (); void dosthelse (string S );}

 
Package dynamicproxy; public class realobject implements simpleinterface {@ overridepublic void dosomething () {system. out. println ("real object do something") ;}@ overridepublic void dosthelse (string s) {system. out. println ("real object do something else:" + S );}}
 
Package dynamicproxy; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; public class dynamicproxyhandler implements invocationhandler {private object; Public dynamicproxyhandler (Object O) {This. object = O ;}@ overridepublic object invoke (Object proxy, method, object [] ARGs) throws throwable {system. out. println ("dynamicproxyhandler process. sent Proxy: "+ proxy. getclass (); Return method. invoke (object, argS );}}
 package dynamicproxy; import Java. lang. reflect. proxy; public class test {static void consumer (simpleinterface OBJ) {obj. dosomething (); obj. dosthelse ("You can handle it");}/*** @ Param ARGs */public static void main (string [] ARGs) {realobject OBJ = new realobject (); consumer (OBJ); simpleinterface proxobj = (simpleinterface) proxy. newproxyinstance (simpleinterface. class. getclassloader (), new class [] {simpleinterface. class}, new dynamicproxyhandler (OBJ); consumer (proxobj) ;}}

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.