Callbacks are widely used in spring , so what is a callback?
Simply put, the C method of class B is called in class A, and then class B calls the D method in class A. Method D is called the callback method . Callbacks are the basis for implementing asynchrony. The classic callback method is as follows:
- Class A implements the callback interface CallBack - background 1
- class A contains a reference b to class B - background 2
- class B has a method f(CallBack callback) whose parameter is callback - background 3
- A's object a calls B's method f(CallBack callback) ——A class calls a method of B class C
-
Then b can call A's method in the f(CallBack callback) method - class B calls a method D of class A
After introducing the concept, let’s give an example. The client initiates a request to the server and sends information. After receiving the client's information, the server sleeps for 5 seconds (simulating a time-consuming operation), and calls back the client after 5 seconds and outputs the request status.
callback interface
Service-Terminal
client
test program
Test Results
The first one, two or three lines are output first, and four or five lines are output after five seconds.