Dynamic Agent technology is a very important part of the entire Java technology system, which is the basis for us to learn more about the Java framework, and is one of the basic knowledge to understand the framework of spring in depth.
The class of the dynamic Proxy in Java has to implement an interface, which means that only the methods defined in the interface implemented by the class can be proxied, which has some limitations in actual programming, and the efficiency of using reflection is not very high. So the Cglib was born.
The use of Cglib to implement dynamic proxy, completely non-agent class must implement the interface constraints, and cglib based on ASM bytecode generation framework, using bytecode technology to generate proxy classes, in theory, than using Java reflection efficiency is higher.
1, cglib dynamic agent concept
For example: singers or stars have their own brokers, the broker is their agent, when we need to find a star performance, can not directly find the star, can only be a star agent. For example Wangbaoqiang in real life is very famous, can sing, can Dance, will be filming. Wangbaoqiang before the famous, in their village farming, we can directly find him singing, dancing, filming, Wangbaoqiang famous, he found a broker, this broker is Wangbaoqiang agent (agent), when we need to find Wangbaoqiang performance, can not directly find Bao Qiang (Wangbaoqiang said, You find my agent!), so the value of Wangbaoqiang this agent exists is to intercept our direct access to Wangbaoqiang!
The original agent requirements and Wangbaoqiang like singing and dancing, but such agents are not many ah, really can not find such agents, then lower the demand, you want to find Wangbaoqiang, I will find out to you.
Wangbaoqiang (Real business object) will sing, Can Dance, will be filming, we can not directly find him to sing, dance, filming, can only find his agent (Agent object) singing, dancing, filming.
2. Some Prerequisites
Additional Import Cglib-2.2.0.jar required
3. Implement target object class using Cglib in Java (concrete implementation object)
PackageCom. CGLib;/** * Wangbaoqiang is an actor, he can sing and dance. * There is no implementation interface * @author Fan Fangming */ Public class Wangbaoqiang{ PrivateString name; Public Wangbaoqiang(){ This. Name ="Wangbaoqiang"; } PublicStringSing(String name) {System.out.println ( This. GetName () +"Start singing."+name+"Song!! ");return "The singing is over, thank you!" "; } PublicStringDance(String name) {System.out.println ( This. GetName () +"Start jumping"+name+"Dance!! ");return "Dance is done, thank you!" "; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; }}
4. proxy class for generating proxy objects in Java using Cglib
PackageCom. CGLib;ImportJava.lang.reflect.Method;ImportNet.sf.cglib.proxy.Enhancer;ImportNet.sf.cglib.proxy.MethodInterceptor;ImportNet.sf.cglib.proxy.MethodProxy;/** * Actor's agent, he can't sing and dance, but brokers can find people who can sing and dance * @author Fan Fangming * * Public class Actorjingjiren implements methodinterceptor{ PublicObjectIntercept(Object obj, method method, object[] arg, Methodproxy proxy)throwsThrowable {System.out.println ("CGLIB before:"+method); System.out.println ("I am his agent, and have something to look for me first!" "); Object object=proxy.invokesuper (obj, arg); System.out.println ("CGLIB after: End." ");returnObject } PublicWangbaoqiangGetProxy() {Enhancer enhancer =NewEnhancer (); Enhancer.setsuperclass (Wangbaoqiang.class); Enhancer.setcallback (NewActorjingjiren ()); Wangbaoqiang object= (Wangbaoqiang) enhancer.create ();returnObject } }
5. Test class to invoke dynamic proxy
PackageCom. CGLib;/** * Test class, find the broker to make a singing and dancing request on the line * * @author Fan Fangming * * Public class proxytest { Public Static void Main(string[] args) {//Find brokers FirstActorjingjiren proxy =NewActorjingjiren ();//Get related actors by broker (Agent object)Wangbaoqiang p = proxy.getproxy ();//Let the actors singString RetValue = p.sing ("No Thief in the world."); System.out.println (RetValue);//Let the actors danceString value = P.dance ("Phoenix Legend"); System.out.println (value); }}
6. Operation Result
CGLIB before:public java.lang.String com. CGLib.WangBaoQiang.sing (java.lang.String)
I'm his agent, I have something to look for first!
CGLIB before:public java.lang.String com. CGLib.WangBaoQiang.getName ()
I'm his agent, I have something to look for first!
CGLIB after: End.
Wangbaoqiang began to sing the world No Thief song!!
CGLIB after: End.
Singing is over, thank you!
CGLIB before:public java.lang.String com. CGLib.WangBaoQiang.dance (java.lang.String)
I'm his agent, I have something to look for first!
CGLIB before:public java.lang.String com. CGLib.WangBaoQiang.getName ()
I'm his agent, I have something to look for first!
CGLIB after: End.
Wangbaoqiang began to jump the Phoenix legendary dance!!
CGLIB after: End.
Dance is done, thank you!
Note: Before performing an action, there are 2 cglib before executed because there is an internal action being executed (GetName ()) because all the actions are intercepted, so there are 2.
A good memory is better than a bad writer. 47-java Interceptor-Dynamic Proxy with Cglib (2)