Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analyze the principle and usage of Java agent

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "analyzing the principle and usage of Java agent". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "analyzing the principle and usage of Java Agent".

1. What is an agent?

Dynamic proxy technology is the most important technology in the whole java technology, it is the basis of learning java framework, do not know dynamic proxy technology, so we can not understand when learning these frameworks of Spring.

Dynamic proxy technology is used to generate a proxy object of an object. Why do you need to generate a proxy object for an object in development? To take a real-life example: singers or stars have their own agent, this agent is their agent, when we need to find a star to perform, we can not directly find the star, we can only find the star's agent. For example, Andy Lau is very famous in real life, singing, dancing and filming. Before Andy Lau is not famous, we can directly ask him to sing, dance and film. After Andy Lau is famous, the first thing he does is to find an agent. This agent is Andy Lau's agent (agent). When we need to find Andy Lau to perform, we can't find Andy Lau directly (Andy Lau said You can only talk to my agent about specific matters, so the value of Andy Lau as an agent is to intercept our direct visit to Andy Lau! This real-world example is the same as in our development, the reason why we want to generate a proxy object for an object is mainly used to intercept access to real business objects. So what method should the proxy object have? The proxy object should have the same method as the target object

Therefore, two concepts of proxy object are made clear here: 1. The value of proxy object is mainly used to intercept access to real business objects. 2. The proxy object should have the same method as the target object (real business object). Andy Lau (real business object) can sing, dance and film. Now we can't directly ask him to sing, dance and film. We can only find his agent (agent) to sing, dance and film. If a person wants to be Andy Lau's agent, then he must have the same behavior as Andy Lau (can sing, dance and film). What can Andy Lau do? He (agent) has to have some way. We ask Andy Lau's agent to sing, dance and film, but the agent does not really know how to sing, dance and film. It is Andy Lau who really knows how to sing, dance and film. The real example is that if we want to find Andy Lau to sing, dance and film, then we can only find his agent and pay his agent, and then the agent will let Andy Lau sing. Dancing, filming.

two。 Classification of agents

Static proxy: the relationship between the proxy class and the delegate class is determined before the code runs, that is, the code for the proxy class already exists at the beginning.

Dynamic proxy: the bytecode of the dynamic proxy class is generated while the program is running.

Introduction to the proxy "java.lang.reflect.Proxy" class in 3.Java

Now that you want to generate a proxy object for an object, the proxy object usually has to write a class to generate it, so first you need to write a class to generate the proxy object. How to use a program to generate a proxy object for an object in java? java provides a "java.lang.reflect.Proxy" class after JDK1.5, which is used to create a proxy object for an object through a newProxyInstance method provided by the "Proxy" class, as shown below:

Static Object newProxyInstance (ClassLoader loader, Class [] interfaces, InvocationHandler h)

The newProxyInstance method is used to return a proxy object. This method has a total of three parameters, ClassLoader loader is used to indicate which class loader is used to generate the proxy object, Class [] interfaces is used to indicate which proxy object is generated, and InvocationHandler h is used to indicate what the generated proxy object is going to do through the interface. So we just need to call the newProxyInstance method to get the proxy object of an object.

Write classes that generate proxy objects

In java, in order to generate a proxy object for an object, the object must have an interface, so the first step is to design the interface of the object and define the behavior (methods) of the object in the interface.

StduentInterface.java-- defines the behavior interface of an object

Public interface StduentInterface {public int insertStudent (Student student);}

StudentDao.java-- defines the target business object class

Public class StudentDAO implements StduentInterface {@ Override public int insertStudent (Student student) {/ / the following code is the business logic return null = = student? 0: 1;}}

Create a static proxy

Public class StudentStaticProxyDAO {private StudentDAO studentDAO; public StudentStaticProxyDAO (StudentDAO studentDAO) {this.studentDAO = studentDAO;} public int insertStudent (Student student) {Logger.info ("start inserting a student record:" + student.toString ()); int number = studentDAO.insertStudent (student); Logger.info ("insert" + number + "student record"); return number;}}

Dynamic agent

Public class LoggerDynamicProxy implements InvocationHandler {private Object target; public LoggerDynamicProxy (Object obj) {this.target = obj;} / * * meaning of the invoke method * * in the dynamic proxy class: * * proxy: proxy object * method: method to be called in the delegate class * args: parameter of the method method * / @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {Logger.info ("before before business method call") Object obj = method.invoke (target, args); Logger.info ("before before business method call"); return obj;}}

Test class

Public class ProxyTest {public static void main (String [] args) {/ * basic component preparation * / StudentDAO studentDao = new StudentDAO (); Student student = new Student (100000); StudentStaticProxyDAO studentProxyDAO = new StudentStaticProxyDAO (studentDao); / * Test the functionality of the delegate class * / System.out.println (studentDao.insertStudent (student)); / * * Test the functionality of the static proxy class * / System.out.println (studentProxyDAO.insertStudent (student)) / * Test the functionality of the dynamic proxy class * / StduentInterface newProxyInstance = (StduentInterface) Proxy.newProxyInstance (studentDao.getClass (). GetClassLoader (), studentDao.getClass (). GetInterfaces (), new LoggerDynamicProxy (studentDao)); int insertStudent = newProxyInstance.insertStudent (student); System.out.println (insertStudent);}

Running result

1INFO: start inserting a student record: Student [id=100000, name=null] INFO: insert a student record 1INFO: before business method call before INFO: before business method call before 1INFO: before business method call INFO: before business method call before 1

At this point, I believe you have a deeper understanding of "analyzing the principle and usage of Java agent". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report