Spring: How does a request scoped bean behave inside a asynchronus method call

930 Views Asked by At

Im working with Spring Boot 2.2.4.

I have the following class, where Client is a request scoped bean:

@AllArgsConstructor
@Component
public class CacheLoader {

  private Client client;

  @Async
  public void load() {
    //do something else
    client.execute();
  }
}

The load method is called in a controller. The request is finished before the execute() method starts in the other thread spawned by the async load() method.

Still the execute method does finish successfully. Why is the request Scoped bean still available even though the request was finished before?

Also was is going to happen if two requests are send. Will the Client which is bound to the second request replace the Client from the first request so that the asynchronous method triggered by the first request will execute the call with the Client from the second request?

0

There are 0 best solutions below