Singelton Bean Serving Concurrent Requests In Spring Boot
We have wonder many a time , how a single spring bean can serve so many concurrent request without corrupting data of Individual Requests .
Lets See this Service Class :-
@Service
class MyService {
@Autowired
MyFacade myFacade;
public void doRun(){
myFacade.doSomething()
}
}
Here MyService Class bean is using the dependency of MyFacade and calling a method of that Class , But we can see that MyService class has no state of itself , whenever the bean of MyService Class will be created , No State will be there in it .
lets See another case :-
@Service
class MyService {
int x = 10;
@Autowired
MyFacade myFacade;
public void doRun(){
myFacade.doSomething();
x++;
}
}
Here we can see our bean has some property which will be shared across threads which will be using its doRun method.