Memory is not as good as bad pen 46-java Interceptor-a thorough understanding of the concept of dynamic proxy (1), pen 46-java

Source: Internet
Author: User

Memory is not as good as bad pen 46-java Interceptor-a thorough understanding of the concept of dynamic proxy (1), pen 46-java

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.

1. Concept of proxy in JAVA

Dynamic proxy technology is used to generate a proxy object of an object. It seems like you are confused.
(The following example is from the Internet)
For example, in real life, stars and stars all have their own brokers who are their agents. When we need to find a celebrity table, we cannot find the Star directly, it can only be an agent for a 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!
This example is the same as that in development. In development, we generate an object proxy object, which is mainly used to intercept access to real business objects.
So what method should a proxy object have? The proxy object should have the same method as the target object.
1. The value of proxy objects is mainly used to intercept access to real business objects.
2. the proxy object should have the same method as the target object (real business object.
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, if a person wants to become Wang Baoqiang, then he must look like Wang Baoqiang (singing, dancing, and filming). How can he (agent) how can we find Wang Baoqiang's agent to sing, dance, and film? But the agent doesn't really know how to sing, dance, or film. He really knows how to sing, dance, or film Wang Baoqiang, in reality, we are looking for Wang Baoqiang to sing, dance, and film. In this case, we can only look for his agent and pay the money to his agent. Then the agent will let Wang Baoqiang sing, dance, and film.

2. Classes and methods for implementing dynamic proxy in JAVA

After JDK1.5, java provides a "java. lang. reflect. Proxy" class. A newProxyInstance method provided by the "Proxy" class is used to create a Proxy object for an object:
Static Object newProxyInstance (ClassLoader loader, Class

3. Define the behavior interface of the object

In java, it is stipulated that to generate a proxy object of an object, this object must have an interface, so the first step is to design the interface of this object, define the behavior (method) of this object in the interface ).

Package com. proxy;/*** an Actor object interface, which must sing and dance * @ author fan fangming */public interface Actor {String sing (String name ); string dance (String name );}
4. Define the target object class (specific implementation object)
Package com. proxy;/*** Wang Baoqiang is an Actor who can sing and dance. * @ author fan fangming */public class WangBaoQiang implements Actor {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 ;}}
5. Define the proxy class for generating proxy objects
Package com. proxy; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy;/*** 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 {// design a class variable remember the proxy class's target object private Actor actor = new WangBaoQiang (); // return the public Actor getProxy () object of Wang Baoqiang through the broker {// use Proxy. newProxyInstance (ClassLoader loader, Class <?> [] Interfaces, // InvocationHandler h) returns the Proxy object return (Actor) Proxy of an object. newProxyInstance (ActorJingJiRen. class. getClassLoader (), actor. getClass (). getInterfaces (), new InvocationHandler () {/*** the InvocationHandler interface only defines one invoke method. Therefore, for such an interface, * We do not need to define a class to implement this interface, but directly use an anonymous internal class to implement this interface. new * InvocationHandler () {} is the anonymous implementation class for the InvocationHandler interface. ** in the invoke method code, specify the returned proxy object. proxy: Pass the proxy object into the proxy. * method: Send the proxy Like the currently called method passed in args: Pass in the method parameter * so we can use method in the invoke method. getName () can be used to determine the Method of the proxy Object currently called */@ Override public Object invoke (Object proxy, method Method, Object [] args) throws Throwable {if (method. getName (). equals ("sing") {System. out. println ("I'm his agent. Please contact me first! "); // The proxy object calls the sing method of the real target object to process the return method of the user request. invoke (actor, args);} if (method. getName (). equals ("dance") {// The proxy object calls the dance method of the real target object to process the user request System. out. println ("I'm his agent. Please contact me first! "); Return method. invoke (actor, args) ;}return null ;}});}}
6. Test class for calling dynamic proxy
Package com. proxy;/*** 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 Actor p = proxy from 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 );}}
7. Running result

I am his agent. Please contact me first!
Wang Baoqiang began to sing the world's no thief song !!
Singing is over. Thank you!
I am his agent. Please contact me first!
Wang Baoqiang started the phoenix legend dance !!
The dance is complete. Thank you!

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.