Bad memory: 47-java Interceptor-implement dynamic proxy with CGLib (2), 47-javacglib

Source: Internet
Author: User

Bad memory: 47-java Interceptor-implement dynamic proxy with CGLib (2), 47-javacglib

Dynamic proxy technology is a very important part of the entire java technical system. It is the basis for us to learn more about the java framework and one of the basic knowledge we need to know about Spring and other frameworks.
The dynamic proxy class in Java must implement an interface, that is, only the methods defined in the interfaces implemented by this class can be proxy, which has certain limitations in actual programming, in addition, reflection is not very efficient. Then CGLIB was born.
Using CGLib to implement dynamic proxy is not restricted by the interface that the proxy class must implement. Besides, CGLib uses the ASM bytecode generation framework at the underlying layer to generate the proxy class using the bytecode technology, theoretically, it is more efficient than using Java reflection.

1. CGLib dynamic proxy Concept

For example, stars and stars all have their own agents. This agent is their agent. When we need to find a celebrity table, we cannot find the Star directly, but only the agent of the star. For example, Wang Baoqiang is very famous in real life. He can sing, dance, and film. Before Wang Baoqiang became famous, we could find him singing, dancing, and filming in the village. After Wang Baoqiang became famous, he found a broker, this agent is the proxy of Wang Baoqiang. When we need to seek a performance from Wang Baoqiang, we cannot find Wang Baoqiang directly (Wang Baoqiang said, look for my agent !), Therefore, the value of Wang Baoqiang's agent is to intercept our direct access to Wang Baoqiang!
The original agent requested that he could sing and dance like Wang Baoqiang, but there were not many such agents. He could not find such an agent, so he lowered the requirement. You need to contact Wang Baoqiang, I will find him for you.
Wang Baoqiang (real business object) can sing, dance, and film. Now we can't directly find him singing, dancing, or filming. We can only find his agent (Agent object) to sing and dance, filming.

2. Some prerequisites

Additional cglib-2.2.0.jar import required

3. Use CGLib in Java to implement the target object class (specific implementation object)
Package com. CGLib;/*** Wang Baoqiang is an actor who can sing and dance. * No interface is implemented here * @ author fan fangming */public class WangBaoQiang {private String name; public WangBaoQiang () {this. name = "Wang Baoqiang";} public String sing (String name) {System. out. println (this. getName () + "Start singing" + name + "song !! "); Return" Singing is over. Thank you! ";} Public String dance (String name) {System. out. println (this. getName () +" "+ name +" !! "); Return" the dance is over. Thank you! ";}Public String getName () {return name;} public void setName (String name) {this. name = name ;}}
4. Use CGLib in JAVA to generate proxy classes for proxy objects
Package com. CGLib; import java. lang. reflect. method; import net. sf. cglib. proxy. enhancer; import net. sf. cglib. proxy. methodInterceptor; import net. sf. cglib. proxy. methodProxy;/*** the actor can't sing or dance, but the broker can find the person who can sing and dance * @ author fan fangming */public class ActorJingJiRen implements MethodInterceptor {public Object intercept (Object obj, Method method, Object [] arg, MethodProxy proxy) throws Throwable {System. out. println ( "CGLIB Before:" + method); System. out. println ("I am his agent. Please contact me first! "); Object object = proxy. invokeSuper (obj, arg); System. out. println (" CGLIB After: end. "); Return object;} public WangBaoQiang getProxy () {Enhancer enhancer = new Enhancer (); enhancer. setSuperclass (WangBaoQiang. class); enhancer. setCallback (new ActorJingJiRen (); WangBaoQiang object = (WangBaoQiang) enhancer. create (); return object ;}}
5. Test class for calling dynamic proxy
Package com. CGLib;/*** test class. Find the broker's requirement for singing and dancing. ** @ author fan fangming */public class ProxyTest {public static void main (String [] args) {// first find the broker ActorJingJiRen proxy = new ActorJingJiRen (); // obtain the relevant actor (proxy object) WangBaoQiang p = proxy through the broker. getProxy (); // Let the actor sing String retValue = p. sing ("no thief in the world"); System. out. println (retValue); // Let the actors dance String value = p. dance ("legend of Phoenix"); System. out. println (value );}}
6. Running result

CGLIB Before: public java. lang. String com. CGLib. WangBaoQiang. sing (java. lang. String)
I am his agent. Please contact me first!
CGLIB Before: public java. lang. String com. CGLib. WangBaoQiang. getName ()
I am his agent. Please contact me first!
CGLIB After: end.
Wang Baoqiang began to sing the world's 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 am his agent. Please contact me first!
CGLIB Before: public java. lang. String com. CGLib. WangBaoQiang. getName ()
I am his agent. Please contact me first!
CGLIB After: end.
Wang Baoqiang started the phoenix legend dance !!
CGLIB After: end.
The dance is complete. Thank you!

Note: Before an action is executed, two CGLIB Before actions are executed because another internal action is executed (getName () because all actions are intercepted, therefore, there are two.

Related Article

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.