in simple terms,Java reflection uses the class name to obtain all the information of this class ; agent, there is a passing process, the class object to other classes, let other classes to proxy their own class object and then do things;
analogy: ordering takeout, delivery of small brother in the hand of the takeaway is my object, I let him send. Static agent is I told the delivery of small brother takeout is mine (assuming that the takeout brother knows me), and then he ran directly to me; Dynamic agent is I don't tell the delivery brother who I am, he does not know me, I only tell him my phone or address, He found a way to find me, and noticed, how could he find me? Is reflection, he used my phone and address, he knew everything about me (exaggerated) can find me.
The following examples illustrate these two closely related points of knowledge:
1. Reflection
In the case of Servlets, for example, in our Web project, there is usually such configuration information in the. xml file.
<Web-app> <servlet> <Servlet-name>Test</Servlet-name> <Servlet-class>Moreservlets. Testservlet</Servlet-class> </servlet> </Web-app>
Then, we deploy the project in the server (tomcat/websphere), note that at this time there is no source code on the server, only the class file, then the server is how to find the Servlet class, and generate object execution method? by reflection, the provided class name (Moreservlets) is leveraged by the server through the reflection mechanism. Testservlet) generates this class in the new object and then uses all the object's contents.
Add: We know that there are two important specifications in the Java EE specification: Jsp/servlet and EJB, and Tomcat only supports the former, that is, if we write a class that follows the EJB specification, deployed on Tomcat, although everyone is in the Java language , but the Tomcat server is also not known because the Tomcat server can only parse classes that conform to the Jsp/servlet specification, and further, even if the Tomcat server restores a class that follows the EJB specification through a reflection mechanism, it still cannot use such objects. Because the specifications are inconsistent. See also:servlet specification ,EJB specification . Imagine that without this technology, XML is not very useful, at least in the above code, the server after parsing, get just a class name, have meaning ...
2. Agent
actually understand the reflection mechanism, the dynamic agent is very simple, static proxy does not involve reflection. The purpose of dynamic proxy is to add some functions on the basis of the original method, without changing the signature of this method, the class that originally called this method will still work normally. The typical case is AOP, and the RCP in Hadoop:.
Static Proxy: The class being proxied can directly and explicitly tell (pass) the proxy class its own specific object.
Dynamic Agent: The proxy class can not directly tell the proxy class about its own object information, intelligence provides some information, the remainder of the proxy class needs to dynamically generate the proxy class according to the existing information.
proxy mode and the principle of the callback function to get together to explain that will be more clear:
callback function: A calls B, and passes A to B. B Executes the method (callback) that invokes a.
The goal is: A initiates a call to B,b to process something, and then calls back to perform some of the actions of a.
The relationship between the two: A and B are association relationships.
The difference between a proxy and a callback:
Agent, proxy class can be represented by the premise of the agent (proxy class to implement the interface of the agent, as my agent, at least with me some important aspect is a stand it! )。 The class that eventually executes my method, looks like me. I am the protagonist.
Callbacks, which ultimately execute the classes of my native methods, do not emphasize the appearance (you can not implement the interfaces I implement). It is emphasized that you must call me when you execute (General callback function, do callback, meaning, Hello!) Your turn! ), and as I am also going to pass on my own object to others. People are the protagonists.
proxies are similar to callbacks: they perform their own inherent methods through others . agent and callback the main concern of others is how to tune you. The proxy invokes its own method, the method name is the same as itself; The method name is not required.
If you want to understand the principles of reflection and dynamic proxy implementation in Java reference: Reflection mechanism and dynamic agent, write very good understanding is in place, give a praise!
Java reflection, Java Proxy mode