Explanation of the method in a single instance

The controller in spring is a singleton. The system will only initialize one instance for each controller, all incoming requests are processed by this instance, and the service layer injected in the controller is also an instance, but each new request comes in and enters the service layer, it will be Start a new transaction for processing, each request entering the service will not affect each other's transactions, each starts a new transaction, but if another service method is called in the service method, this will The propagation level of the transaction is used. If the service1 method in the service calls the service2 method, and there is already a transaction in the service1, whether to start a new transaction in the service2, the propagation level configured in the project is generally REQUIRED. This propagation level means that the transaction in service1 will be used, and the transaction will not be restarted for the service2 method.


A singleton just says that the created controller and service object instances are one.
For the same controller example, each time a new request comes in, each call is a separate thread, and the transaction attributes, parameter values, etc. in the specific method are visible to the current thread. So they don't affect each other.

Only static class attributes are shared by multiple threads.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326990464&siteId=291194637