In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Java dynamic agent example analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java dynamic agent example analysis" bar!
Define
Dynamic proxy means that the relationship between the agent class and the target class is determined when the program is running. the client invokes the method of the target object through the proxy class, which is to dynamically create the proxy object of the target class when the program is running.
classification
Jdk dynamic agent
Cglib dynamic agent
Case requirement
Apple sells phones through Apple agents.
Scheme 1: jdk dynamic agent
Define an abstract interface
/ * the interface for selling mobile phones (proxy mode-abstract roles) * @ author:liyajie * @ createTime:2022/2/22 14:42 * @ version:1.0 * / public interface IPhone {/ * selling mobile phones * @ author:liyajie * @ date: 2022-2-22 14:44 * @ param * @ return void * @ exception: * @ update: * @ updatePerson: * / void sellPhone () }
Define the target class implementation interface and override the interface method
/ * Apple (proxy mode-target role) * @ author:liyajie * @ createTime:2022/2/22 14:46 * @ version:1.0 * / public class TargetPhone implements IPhone {@ Override public void sellPhone () {System.out.println ("Apple is selling phones");}}
Define a proxy class
/ * * Agent (agent mode-agent role) * @ author:liyajie * @ createTime:2022/2/22 14:50 * @ version:1.0 * / public class ProxyPhone {private Object target; public ProxyPhone (Object target) {this.target = target } public Object getProxyInstance () {return Proxy.newProxyInstance (target.getClass () .getClassLoader (), target.getClass () .getInterfaces (), new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println ("before JDK dynamic Agent starts Add business logic XXX ") / / the method of using reflection mechanism to call the target object: problem 2: Object invoke = method.invoke (target, args); System.out.println ("add business logic XXX after JDK dynamic proxy ends"); return invoke });}}
Define test classes
/ * * Test class * @ author:liyajie * @ createTime:2022/2/23 15:15 * @ version:1.0 * / public class Test {public static void main (String [] args) {/ / create target class TargetPhone targetPhone = new TargetPhone (); / / create proxy class IPhone iPhone = (IPhone) new ProxyPhone (targetPhone). GetProxyInstance (); System.out.println (iPhone.getClass ()) / / call the target method iPhone.sellPhone () through the proxy class;}}
View test results
Scheme 2: cglib dynamic agent
Define the target class
/ * Apple (proxy mode-target role) * @ author:liyajie * @ createTime:2022/2/22 14:46 * @ version:1.0 * / public class TargetPhone {public void sellPhone () {System.out.println ("Apple is selling phones");}}
Define an agent factory that is used to get the proxy class
/ * Agent Factory * @ author:liyajie * @ createTime:2022/2/23 15:32 * @ version:1.0 * / public class ProxyFactory implements MethodInterceptor {private Object target; public ProxyFactory (Object target) {this.target = target;} public Object getProxyInstance () {/ / create a utility class Enhancer enhancer = new Enhancer () / / set parent class enhancer.setSuperclass (target.getClass ()); / / set callback function enhancer.setCallback (this); / / create subclass object, that is, proxy object return enhancer.create () } @ Override public Object intercept (Object o, Method method, Object [] objects, MethodProxy methodProxy) throws Throwable {System.out.println ("add business logic xxxx before cglib dynamic agent starts"); Object invoke = method.invoke (target, objects); System.out.println ("add business logic after cglib dynamic agent"); return invoke;}}
Define test classes
/ * Test class * @ author:liyajie * @ createTime:2022/2/23 15:44 * @ version:1.0 * / public class Test {public static void main (String [] args) {/ / create target object TargetPhone targetPhone = new TargetPhone (); / / get proxy object TargetPhone proxyInstance = (TargetPhone) new ProxyFactory (targetPhone). GetProxyInstance () / / call the specific method proxyInstance.sellPhone () through the proxy object;}}
View test results
Analysis.
First of all, we can see that whether it is a jdk dynamic proxy or a cglib dynamic proxy, the effect of the implementation is exactly the same as the static proxy, and the function is extended. However, there are some differences between the two dynamic agents, in which the jdk dynamic proxy needs the target object implementation interface, but the cglib dynamic proxy does not need it, because it builds a subclass object in memory to extend the function of the target object.
Summary
Through a case study, we have learned several ways to implement the agent pattern. Let's summarize this pattern:
Advantages:
The proxy pattern acts as an intermediary and protection between the client and the target object.
The proxy object can expand the function and business of the target object, and enhance the target object.
The proxy mode can separate the client from the target object, which reduces the coupling degree of the system to a certain extent.
Disadvantages:
The request needs to go through the proxy object, which will slow down the processing speed.
Because there will be a large number of proxy objects, which will increase the complexity of the system.
Thank you for your reading, the above is the content of "Java dynamic agent example analysis", after the study of this article, I believe you have a deeper understanding of the Java dynamic agent example analysis of this problem, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.