Import cglib-nodep-2.1_3.jar
Package com. jxnu. cglib; public class Person {public void m1 () {System. out. println ("method 1 executed");} public void m2 () {System. out. println ("method 2 executed ");}}
Package com. jxnu. cglib; import java. lang. reflect. method; import net. sf. cglib. proxy. callback; import net. sf. cglib. proxy. enhancer; import net. sf. cglib. proxy. methodInterceptor; import net. sf. cglib. proxy. methodProxy; public class PersonDemo {public static void main (String [] args) {final Person p = new Person (); // return the dynamic proxy object of p: this proxy object is a subclass of Person. Person pp = (Person) Enhancer. create (p. getClass (), new MethodInterceptor () {@ Overridepublic Object intercept (Object proxy, Method method, Object [] arg2, MethodProxy arg3) throws Throwable {long time = System. nanoTime (); // nanosecond 1 millisecond = 1000 nanosecond Object obj = method. invoke (p, arg2); System. out. println (method. getName () + "running time:" + (System. nanoTime ()-time) + "nanosecond"); return obj ;}}); pp. m1 (); pp. m2 ();}}
It intercepts all methods of the person class. It is a dynamic method that is uncertain.
Output result:
Method 1 is executed
M1 running time: 262234 nanoseconds
Method 2 is executed
M2 running time: 49461 nanoseconds
Zookeeper