Hadoop technology insider HDFS-Note 5: Dynamic proxy

Source: Internet
Author: User
1.1. Java Dynamic proxy

Proxy (create proxy object) and invocationhandler (call forwarding) under java. Lang. Reflect package

The proxy object represents the target object to execute the corresponding activity.

Static Proxy: the proxy object implements the same interface of the target object, implements proxy interface and call forwarding (not recommended)

The additional logic before and after the call reflects the value of the proxy object

Sample Code:

1. Java Dynamic proxy interface package proxy; public interface bookdao {// Add public void addbook ();} 2. Create an implementation class (target class) package proxy; public class bookdaoimpl implements bookdao {@ overridepublic void addbook () {// todo auto-generated method stubsystem. out. println ("add the implementation method of books .... ") ;}} 3. Create a proxy package proxy; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy; // JDK dynamic proxy class implements public class bookdaoproxy implements invocationhandler {// actual proxy object private object target; // bind the delegate object and return a proxy class public object BIND (Object targetdomainthis.tar get = target; // get the proxy object // interface to be bound (this is a defect, cglib makes up for this defect) return proxy. newproxyinstance (this. getclass (). getclassloader (), this.tar get. getclass (). getinterfaces (), this) ;}@ overridepublic object invoke (Object proxy, method, object [] ARGs) throws throwable {// todo auto-generated method stubobject result = NULL; system. out. println ("START transaction processing... "); // execution method result = method. invoke (target, argS); system. out. println ("transaction end processing... "); return result;} 4. Test class: Package proxy; public class booktest {public static void main (string [] ARGs) {// obtain a dynamic proxy class bookdao proxy = (bookdao) New bookdaoproxy (). BIND (New bookdaoimpl (); // call the proxy method proxy. addbook ();}}

 

1.1. Java NiO (self-Research)

Network Programming

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.