In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the concept of java static agent and dynamic agent". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Static agents: generate proxies at compile time, also known as compile-time enhancements.
two。 Dynamic proxies: runtime generation proxies, which also become runtime enhancements.
Dynamic proxy: there are two common ways to implement dynamic proxy.
A.cglib dynamic proxy. The proxy is the class, and there is no need to implement the interface. Proxies are implemented through subclasses of derived classes. The purpose of modifying the class is achieved by dynamically modifying the bytecode at run time.
B.jdk dynamic proxy. Implement the invoker method of the InvocationHandler-like interface, but note that it is the interface that is proxied. In other words, the business class must implement the interface and get the proxy object through the newProxyInstance in Proxy.
In the java.lang.reflect package, you provide a Proxy class and InvocationHandler interface through which you can generate dynamic proxies for JDK.
1. Create your own call handler by implementing the InvocationHandler interface
InvocationHandlerImpl implements the InvocationHandler interface and the dispatch forwarding of method calls from the proxy class to the delegate class InvocationHandler handler = new InvocationHandlerImpl (..)
2. Create a dynamic proxy class by specifying a ClassLoader object and a set of interface for the Proxy class
/ / dynamically create a class object of the proxy class Class clazz = Proxy.getProxyClass (classLoader, new Class [] {Interface.class,...}) for a set of interfaces, including the Interface interface, through Proxy
3. The constructor of the dynamic proxy class is obtained through the reflection mechanism, and its only parameter type is the calling processor interface type.
/ / get the constructor object Constructor constructor = clazz.getConstructor (new Class [] {InvocationHandler.class}) from the generated class object by reflection
4. A dynamic proxy class instance is created through the constructor, and the calling processor object is passed as a parameter during construction.
/ / create a dynamic proxy class instance Interface Proxy = (Interface) constructor.newInstance (new Object [] {handler}) through the constructor object
In order to simplify the object creation process, the newProxyInstance in the Proxy class encapsulates 2-4 steps, which only takes two steps to complete the creation of the proxy object.
/ / directly create a dynamic proxy class instance through Proxy Interface proxy = (Interface) Proxy.newProxyInstance (classLoader, new Class [] {Interface.class}, handler); this is the end of the introduction of "the concept of java static proxy and dynamic proxy". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.