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 introduces the relevant knowledge of "how to use Java to achieve 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!
Catalogue
Preface
Static agent
Dynamic agent
Dynamic proxy based on CGLib
Summary
Preface
Dynamic proxy is widely used in Java, such as Spring AOP, Hibernate data query, back-end mock of test framework, RPC remote call, Java annotation object acquisition, log, user authentication, global exception handling, performance monitoring, and even transaction processing.
Proxy, refers to the use of proxy objects instead of access to other objects, to put it simply, the intermediary you are looking for when you are looking for a job is an agent, so how does the agent reflect in Java?
Static agent
First of all, we need to know, what is a static agent? A static proxy refers to enhancements to the methods of the target object at compile time, such as:
Public class TestDemo {interface EmailService {void sendEmail (String emailContent);} static class EmailServiceImpl implements EmailService {@ Override public void sendEmail (String emailContent) {System.out.println ("sent an email saying:" + emailContent);}} public static void main (String [] args) {EmailService emailService = new EmailServiceImpl (); emailService.sendEmail ("hello");}}
Now if you want to get the current time before sending a message, you can use the proxy class to enhance the method of sending mail:
Public class TestDemo {interface EmailService {void sendEmail (String emailContent);} static class EmailServiceImpl implements EmailService {@ Override public void sendEmail (String emailContent) {System.out.println ("sent an email saying:" + emailContent);}} static class EmailProxy implements EmailService {private final EmailService emailService; public EmailProxy (EmailService emailService) {this.emailService = emailService @ Override public void sendEmail (String emailContent) {System.out.println (LocalDateTime.now ()); emailService.sendEmail (emailContent);}} public static void main (String [] args) {EmailService emailProxy = new EmailProxy (new EmailServiceImpl ()); emailProxy.sendEmail ("hello");}}
The shortcomings of static agents are very obvious, it is troublesome to write, and the scalability is not strong, while the emergence of dynamic agents will completely solve these problems.
Dynamic agent
A dynamic proxy is the opposite of a static proxy, which enhances a method of the target object at run time, such as a service that still sends mail, which can be achieved by using a dynamic proxy:
Public class TestDemo {interface EmailService {void sendEmail (String emailContent);} static class EmailServiceImpl implements EmailService {@ Override public void sendEmail (String emailContent) {System.out.println ("sent an email saying" + emailContent);}} public static void main (String [] args) {EmailService emailService = new EmailServiceImpl () EmailService emailProxy = (EmailService) Proxy.newProxyInstance (EmailServiceImpl.class.getClassLoader (), EmailServiceImpl.class.getInterfaces (), new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println (LocalDateTime.now ()); Object result = method.invoke (emailService, args); return result ); emailProxy.sendEmail ("hello");}}
Dynamic proxies can be easily implemented using the Proxy and InvocationHandler classes provided by JDK, but this approach also has limitations, that is, the enhanced class must implement the interface, because the Proxy parameters need to receive the interface information of the class.
Dynamic proxy based on CGLib
The emergence of CGLib broke the deadlock, using CGLib, can enhance arbitrary object methods, even if you do not implement any interface, because it is enhanced through inheritance.
Let's demonstrate how to use CGLib, starting with the introduction of dependencies:
Cglib cglib 3.3.0
The implementation is as follows:
Public class TestDemo {static class EmailServiceImpl {public void sendEmail (String emailContent) {System.out.println ("sent an email saying:" + emailContent);} public static void main (String [] args) {EmailServiceImpl emailService = new EmailServiceImpl () EmailServiceImpl emailProxy = (EmailServiceImpl) Enhancer.create (emailService.getClass (), new MethodInterceptor () {@ Override public Object intercept (Object o, Method method, Object [] args, MethodProxy methodProxy) throws Throwable {System.out.println (LocalDateTime.now ()); Object obj = methodProxy.invokeSuper (o, args); return obj;}}) EmailProxy.sendEmail ("hello");}}
It is written in a way similar to that provided by JDK, by enhancing an object through the create () method of the Enhancer class, passing in the object's Class object and an implementation class of the MethodInterceptor interface, and enhancing the original method in the intercept () method.
This is the end of the content of "how to implement dynamic agents with Java". 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.