Dynamic proxy application
1. Permission interception and judgment;
2. Enhanced object functions (methods.
Meaning of proxy:
1. Value of proxy objects: it is mainly used to intercept access to real business objects.
2. ProxyInterfaceAll methods under;
Format: proxy. newproxyinstance (Class Loader, interface implemented by the proxy object, proxy what );
When using this method to generate a proxy object, three parameters are required:
1. Generate the Class Loader used by the proxy object
2. Specify the proxy object of the generated object through the interface: the class of the object must implement an interface;
Proxy who: a specific object or object. After proxy, you can find another object to do this !!!
3. What to do in the method of the generated proxy object is specified by the implementation of the handler interface written by the developer.
Proxy type:
1 package COM. mzj. practice. test; 2 3 Import Java. lang. reflect. invocationhandler; 4 Import Java. lang. reflect. method; 5 import Java. lang. reflect. proxy; 6 7 Import Org. slf4j. logger; 8 Import Org. slf4j. loggerfactory; 9 10 public class xproxy {11 private final logger log = loggerfactory. getlogger (this. getclass (); 12 13 private object target; // target object 14 15/** 16 * constructor 17 * @ Param Target target object 18 */19 P Ublic xproxy (object target) {20 super (); 21 this.tar get = target; 22} 23 24 // Private MX Chun = new MX (); 25 // 26 // public object getproxy () {27 // return proxy. newproxyinstance (xproxy. class. getclassloader (), Chun. getclass () 28 //. getinterfaces (), new invocationhandler () {29 // @ override30 // public object invoke (Object proxy, method, object [] ARGs) 31 // throws throwable {32 // If (meth OD. getname (). equals ("dance") {33 // system. out. println ("pay... "); 34 // return method. invoke (Chun, argS); 35 //} 36 // If (method. getname (). equals ("sing") {37 // system. out. println ("pay a lot of money... "); 38 // return method. invoke (Chun, ""); 39 //} 40 // return NULL; 41 //} 42 //}); 43 //} 44 45 public object getproxy () {46 Return proxy. newproxyinstance (xproxy. class. getclassloader (), target. getclass () 47. geti Nterfaces (), new invocationhandler () {48 @ override49 public object invoke (Object proxy, method, object [] ARGs) 50 throws throwable {51 if (method. getname (). equals ("dance") {52 log.info (method. getname () + "before calling... "); 53 system. Out. println (" pay... "); 54 after calling log.info (method. getname () +... "); 55 return method. invoke (target, argS); 56} 57 if (method. getname (). equals ("sing") {58 log.info (method. getname () + "before calling... "); 59 system. Out. println (" pay a lot of money... "); 60 log.info (method. getname () +" after calling... "); 61 Return method. invoke (target, ""); 62} 63 return NULL; 64} 65}); 66} 67 68 public static void main (string [] ARGs) {69 MX Chun = new MX (); // proxy object 70 xproxy proxy = new xproxy (Chun); 71 person Mingxing = (person) proxy. getproxy (); // It must point to the interface; 72 Mingxing. dance ("Hop"); 73 Mingxing. sing ("Hop"); 74 75} 76}
View code
Interface:
1 package COM. mzj. practice. test; 2/** 3 * copyright (c ), HTF <br> 4 * methods through which the proxy can call the object are open to the public through the interface 5*6 * @ author muzhongjiang 7 * @ Date August 8 */9 public interface person {10 11 void Dance (string ); 12 13 void sing (string); 14 15}
View code
Proxy class:
1 package com.mzj.practice.test; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 6 public class MX implements Person { 7 8 private final Logger LOG = LoggerFactory.getLogger(this.getClass()); 9 10 @Override11 public void dance(String string) {12 LOG.info("string=【" + string + "】");13 }14 15 @Override16 public void sing(String string) {17 LOG.info("string=【" + string + "】");18 }19 20 public void selfMeth(String string) {21 LOG.info("string=【" + string + "】");22 }23 24 }
View code
JDK dynamic proxy