In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to realize Java static agent and dynamic agent". The editor shows you the operation process through the actual case, the operation method is simple, fast and practical. I hope this article "how to realize Java static agent and dynamic agent" can help you solve the problem.
Agent mode
Proxy pattern (Proxy): provides a proxy for other objects to control access to this object.
The main solution: the problem caused by direct access to the object, for example, the object to be accessed is on the remote machine. In object-oriented systems, direct access to some objects will bring a lot of trouble to consumers or system structures for some reasons (such as high overhead in object creation, or some operations require security control, or out-of-process access). We can add an access layer to this object when we access this object.
The elements of the proxy pattern are: common interface, proxy object, and target object.
The behavior of the proxy pattern: the method of executing the target object by the proxy object and the method of extending the target object by the proxy object.
The macro characteristics of the proxy pattern: only the interface is exposed to the client, not the architecture below it.
There are many advantages: there is a layer of isolation in the middle, which is more in line with the principle of opening and closing.
Create an interface
/ * * @ Author: Promsing * @ Date: 2021-4-3-8:25 * @ Description: API for buying a car * @ version: 1.0 * / public interface BuyCar {public void buyCar ();}
Create an implementation class
/ * * @ Author: Promsing * @ Date: 2021-4-3-8:25 * @ Description: implementation class * @ version: 1.0 * / public class BuyCarImpl implements BuyCar {@ Override public void buyCar () {System.out.println ("I want to buy a car");}} static agent:
Create a proxy class
/ * * @ Author: Promsing * @ Date: 2021-4-3-8:26 * @ Description: proxy class * @ version: 1.0 * / public class BuyCarProxy implements BuyCar {private BuyCar buyCar; / / Note the keyword modified by final cannot be modified / / constructor injection, and the object to be proxied is public BuyCarProxy (final BuyCar buyCar) {this.buyCar = buyCar } / / static proxy-the implementation of @ Override public void buyCar () {System.out.println ("No loan, full payment! Preparation before buying a car); buyCar.buyCar (); System.out.println ("after buying a car, go out for a ride");}}
Client call
/ * * @ Author: Promsing * @ Date: 2021-4-3-8:36 * @ Description: client call * @ version: 1.0 * / public abstract class ProxyTest implements BuyCar {public static void main (String [] args) {System.out.println ("- + normal call-+"); BuyCar car=new BuyCarImpl (); car.buyCar () System.out.println ("- + use static proxy-+"); BuyCar proxy=new BuyCarProxy (car); proxy.buyCar ();}}-+ normal call-+ I want to buy a car ~ ~ la-+ use static proxy-+ no loan, full payment! The preparation before buying a car ~ I want to buy a car ~ ~ after buying a car, go out and act as a dynamic agent:
Dynamic proxy class based on interface
Features: bytecode is created on demand and loaded on demand
Function: enhance the method without modifying the source code
Classes involved: Proxy officially provided by JDK
How to create a proxy object: use the newProxyInstance method in the Proxy class
Requirements for creating a proxy object: the proxied class implements at least one interface
Parameters of the newProxyInstance method
ClassLoader: class loader, which is the same as loading the bytecode of the proxied object
Class []: bytecode array-used to make the proxy object and the proxied object have the same method
InvocationHandler: used to provide enhanced code
/ * * @ Author: Promsing * @ Date: 2021-4-3-9:09 * @ Description: description * @ version: 1.0 * / public class DynamicProxy implements InvocationHandler {private BuyCar object; public DynamicProxy (BuyCar object) {this.object = object } / * @ param proxy proxy object reference * @ param method currently executing method * @ param args parameters required for the current execution method * @ return and the proxied object method have the same return value * @ throws Throwable * / @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println ("No loan, full payment! Preparation before buying a car; Object result = method.invoke (object, args); System.out.println ("after buying a car, go out for a ride"); return result;}}
Client
Public static void main (String [] args) {System.out.println ("- + uses interface-based proxy-+"); / / method 1. If you do not write the dynamic proxy class DynamicProxy, you can declare a final-decorated object / * final BuyCarImpl car=new BuyCarImpl () here using the inner class / / BuyCar proxy= (BuyCar) Proxy.newProxyInstance (car.getClass (). GetClassLoader (), car.getClass (). GetInterfaces (), new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println ("No loan, full loan! Preparation before buying a car; Object result = method.invoke (car, args); System.out.println ("buy a car, go out for a ride"); return result;}}); proxy.buyCar (); * / / method 2. Use the DynamicProxy class / / to declare a final-decorated object final BuyCarImpl car=new BuyCarImpl () BuyCar proxy= (BuyCar) Proxy.newProxyInstance (car.getClass (). GetClassLoader (), car.getClass (). GetInterfaces (), new DynamicProxy (car)); proxy.buyCar ();}
Dynamic Agent based on subclass
Features: bytecode is created on demand and loaded on demand
Function: enhance the method without modifying the source code
Classes involved: Enhancer provided by third-party cglib
How to create a proxy object: use the create method in the Enhancer class
Requirements for creating a proxy object: the proxied class cannot be the final class
Parameters of the newProxyInstance method
Class: bytecode used for the specified proxy object
InvocationHandler: a method for providing enhancements
Public static void main (String [] args) {/ / use subclass-based dynamic agents / / need to introduce Jar package-- cglib this case uses cglib3.3.0 System.out.println ("- + use subclass-based agents-+"); final BuyCarImpl car=new BuyCarImpl () BuyCar proxy= (BuyCar) Enhancer.create (car.getClass (), new MethodInterceptor () {@ Override public Object intercept (Object o, Method method, Object [] objects, MethodProxy methodProxy) throws Throwable {System.out.println ("No loan, full loan! Preparation before buying a car); Object result = method.invoke (car, args); System.out.println ("after buying a car, go out for a ride"); return result;}}); proxy.buyCar ();} on "how to achieve static and dynamic agents for Java" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.