JDK Dynamic agent and Cglib dynamic agent
1.JDK Dynamic Agent
The dynamic proxy for the JDK is very simple to use, but it has a limitation that objects that use dynamic proxies must implement one or more interfaces.
Interface 1 Man.java
Interface 1public interface Mans {public void work (); public void Run ();
Interface 2 Women.java
Interface 2public Interface Women { public void Shopping (); }
Implement the interface class Person.java
Public class people implements mans, women {public void Run () { System.out.println ("Men: Brothers run for the second quarter"); public void work () { System.out.println ("Man: Hard Work"), public void shopping () { System.out.println ("Woman: Nicely Dressed");}
proxy class Dynamicproxy.java
import java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.proxy;public class Dynamicproxy implements Invocationhandler {//need to be referenced by proxy class private Object object;//Construction Method public Dynamicproxy (Object object) {This.object = object,} public Object GetProxy () {//Newproxyinstance method through the proxy class Dynamically generates a dynamic proxy and returns it//java also allows this dynamically generated $PROXY0 class to implement all interfaces to the implementation of the proxy class, and inherits the proxy interface. Get is actually a class called $proxy0 extends Proxy implements ***interface class return Proxy.newproxyinstance (Object.getclass (). getClassLoader (), Object.getclass (). Getinterfaces (), this); }//Override the Invoke method, which handles the real method call public object invoke (object proxy, Method method, object[] args) throws Throwable {befor Edoing (); Object Invoke=method.invoke (object, args); Afterdoing (); return invoke; } public void Beforedoing () {System.out.println ("-------------before---------------"), public void afterdoing () {Syst Em.out.println ("-------------after----------------"); }}
Test class Test.java
public static void Main (string[] args) { people Realsubject=new people (); Dynamicproxy dynamicproxy=new Dynamicproxy (realsubject); Man man= (Man) dynamicproxy.getproxy (); Man.work (); Man.run (); Women women= (Women) dynamicproxy.getproxy (); Women.shopping (); }
Run results
| -------------before---------------man: Working hard-------------after-----------------------------before---------------man: Brother, run. Second season-------------after-----------------------------before---------------woman: The-------------after---------------- |
2.CGlib Dynamic AgentCglib is a powerful, high-performanceCode generationpackage. It is widely used by many AOP frameworks, such as spring AOP and DYNAOP, to provide them with methods of interception (interception). The most popular or mapping tool hibernate also uses the Cglib to proxy single-ended single-ended (many-to-one-to-single) associations (deferred fetching of collections, implemented by other mechanisms). Easymock andJmockis a package that tests Java code by using a mock (mock) object. They all create mock-up objects for classes that do not have interfaces by using Cglib.
Implementing Class Man.java
public class Mans {public void works () {System.out.println ("going to Work");}}
proxy class Cglib.java
Import Java.lang.reflect.method;import Net.sf.cglib.proxy.enhancer;import net.sf.cglib.proxy.MethodInterceptor; Import Net.sf.cglib.proxy.methodproxy;public class Cglib implements Methodinterceptor {private Object Target;public Object GetInstance (Object target) {this.target=target; Enhancer enhancer=new Enhancer (); Enhancer.setsuperclass (This.target.getClass ());//callback Method Enhancer.setcallback (this) ;//Create proxy object return Enhancer.create ();} /** * Interceptor, where you can do whatever you please */public object intercept (Object obj, method, object[] Args,methodproxy proxy) throws Throwabl e {System.out.println ("----punch------before going to work"); Proxy.invokesuper (obj, args); SYSTEM.OUT.PRINTLN ("----play after work to-------"); return null;}}
Test class Test.java
public class Test {public static void main (string[] args) {//proxy class Cglib cglib = new Cglib ();//Implementation class man Impl = (man) cglib.ge Tinstance (New Man ()); Impl.work ();}}
Operation Result:
| ----punch in before going to work------go to work----play ball after work------- |
Reference Blog: http://blog.csdn.net/hintcnuie/article/details/10954631
http://www.iteye.com/topic/683613/
I'm a rookie, I'm on my way.May 28, 2015 14:49:44
Java Reflection mechanism &annotation detailed _ii