Simple example of dynamic proxy for interface JavaSE8 Base Proxy

Source: Internet
Author: User
Tags stub

Li Wu:
Learn to think more, honouring teachers save Thanksgiving. Leaf See root Three return one, living water Qingyuan hidden in zero.
Meekness The Lord of Conscience, Haoyuedangkong the King of Freedom. Willing to give the most ascetic no regrets, sincere advice and the line and cherish.



Os:windows7 x64
Jdk:jdk-8u131-windows-x64
Ide:eclipse Oxygen Release (4.7.0)



Interface class:

Package blog.jizuiku6;/** * * *  @author Jizuiku * @version V17.10.03 */public interface Userdao {void play (); void Sleep ();}


Interface Implementation class:

Package blog.jizuiku6;/** * * *  @author Jizuiku * @version V17.10.03 */public class Userdaoimpl implements Userdao {@Overridepublic void Play () {//TODO auto-generated method StubSystem.out.println ("Play");} @Overridepublic void Sleep () {//TODO auto-generated method StubSystem.out.println ("Sleep");}}

The key classes appear:

Package Blog.jizuiku6;import Java.lang.reflect.invocationhandler;import java.lang.reflect.method;/** * * *  @author Jizuiku * @version V17.10.03 */public class Myinvocationhandler implements Invocationhandler {private Object Targe t = null;//Cancel parameterless construction Private Myinvocationhandler () {//TODO auto-generated constructor stub}public Myinvocationhandler (obje CT target) {this.target = target;} @Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {//TODO auto-generated Method stub//Prepare part SYSTEM.OUT.PRINTLN ("Reading document");//This line of code is equivalent//result = Target.method (AGRS); Object result = Method.invoke (target, args);//Closing part System.out.println ("Archiving"); return result;}}

Test class:

Package Blog.jizuiku6;import java.lang.reflect.proxy;/** * * *  @author Jizuiku * @version V17.10.03 */public Class Demo {public static void main (string[] args) {Userdao ud = new Userdaoimpl (); Myinvocationhandler handler = new Myinvocationhandler (UD); ClassLoader loader = Ud.getclass (). getClassLoader (); Class[] interfaces = Ud.getclass (). getinterfaces ();//The method in the interface Userdao proxy = (Userdao) proxy.newproxyinstance (loader, interfaces, handler); Execution of all methods, there will be an additional action before execution, there will be additional action after execution//sometimes use the agent is: Before executing the permission check, after executing the log record//And this program, just for a simple demonstration. Do not have practical value//If you want to use in the work, there is a mature framework can be used. But original aim, pierced will return to the Pope! Javaproxy.play ();p roxy.sleep ();p roxy.hashcode ();}}


Results:

Java is good, worth learning.
Learning resources: Itcast and Itheima Video library. If you have public resources, can share to me, with your resources to learn also can.
Blog post is to watch the video, into thinking written. It's good that the teacher speaks well. Blog bad, is to give the most bitter not serious.

Simple example of dynamic proxy for interface JavaSE8 Base 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.