In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the relevant knowledge points of Java". In daily operation, I believe that many people have doubts about what knowledge points related to Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what knowledge points related to Java"! Next, please follow the editor to study!
What is reflex?
In general, the premise of the need for a function is what problems you encounter, first list some problems, and then how to solve them through reflection. The most common problem encountered by ordinary developers is the need to generate proxy objects. The solution is to merge the classes that need to be strengthened by reflection loading with the supplementary logic to produce a new object, which is the proxy object, that is, the enhanced class with the original class and the new logic. For example, there is an eat method in the Man class. We want to perform hand washing and dishes washing before and after executing the eat method, but cannot modify the eat method. At this time, we need to use a proxy object to perform these operations before and after executing the eat.
Application scenario
In order to ensure the versatility of the framework, they may need to load different objects and classes according to the configuration file and call different methods. At this time, you need to use reflection to dynamically load the required objects at run time. Dynamic proxy, in aspect programming, needs to intercept specific methods, usually using dynamic proxy, and dynamic proxy needs to be implemented using reflection technology. Comments: annotations also use a reflection mechanism, according to the annotation tag to call the annotation interpreter, execute the behavior, if there is no reflection mechanism, the annotation will be invalid. Extensible functionality, applications can create extensible object instances by using fully qualified names.
Terms involved in reflection and proxy
Real object: the object produced by the instantiation of the original class, which is not enhanced by the proxy mode. Proxy object: an object enhanced by proxy mode. Dynamic proxy class: the proxy object logic processor, that is, where the enhanced logic is located, needs to pass in the real object to produce the associated dynamic proxy object. InvocationHandler interface: the dynamic proxy class needs to implement this interface and override the invoke method. Enhanced logic in the invoke method, each instance of the proxy class is associated with a Handler. When we call the proxy object, it will be forwarded to the invoke method of the invocationHandler interface for the call. Proxy: proxy class, which is used to generate a proxy object after a dynamic proxy object is passed in.
The relationship between reflection and Agent
The main function of proxy pattern produces proxy object to realize the enhanced method. Reflection, as a feature provided by Java, is the basis of realizing proxy pattern, that is, using reflection technology to obtain and manipulate classes in Java programs, so as to process these classes in a wrapper and produce proxy objects. Get the proxy object: step 1: call Proxy.newProxyInstance to get a dynamic proxy object that receives three parameters, each of which is
ClassLoader object, which defines which ClassLoader object to generate the proxy object to load
An array of Interface objects that indicates what interface we need to provide to the proxy object, and if we provide it with a set of interfaces, then the proxy object implements that interface using polymorphism. The methods in this set of interfaces are called through polymorphism. 3. An InvocationHandler implementation class object represents which invocationHandler implementation class object I, a dynamic proxy object, will be associated with when calling a method. Step 2: get the class object of the proxy object step 3: get all the methods of the proxy class step 4: call the method of the implementation class through the proxy object, trigger our key step, the invoked method of the implementation class of the invocationHandler interface, thus execute the method of the implementation class. Step 5: three parameters are passed in when calling the invocationHandler API, which are:
Proxy refers to the real person we represent.
Method the methods we need to call
Parameters passed in for args
The difference between jdk dynamic Agent and CGLIB dynamic Agent
Proxy method: the enhanced logic is added by inheriting the class of the real object or implementing the interface it needs to implement. Jdk dynamic proxy is accomplished through the implementation interface. When a class is generated through the implementation interface, it is called jdk dynamic proxy. CGLIB dynamic proxy is accomplished by inheriting classes. When a class does not implement an interface, it can only use jdk dynamic proxy.
Reflection framework
Java provides classes that reflect and obtain the properties and methods of the class, and you need to get the class to operate accordingly, but the reflection framework, Reflections, can obtain not only the classes under the classpath, but also according to specific annotations. Reflections indexes metadata by scanning classpath, and allows metadata to be queried when allowed. Using Reflections, you can easily get the following metadata
All subclasses of a type
As long as there are specific annotations on the types, constructors, methods, and fields, you can get all the information with this annotation.
Get all resources that match a regular expression
Get a method with a specific signature, including parameters, parameter annotations, and return types.
Get the name of the method
Get all the fields in the agent, method names, and the use of the constructor
At this point, the study of "what are the relevant knowledge points of Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.