Sitemap
Javarevisited

A humble place to learn Java and Programming better.

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.

In Conclusion , To make our Spring Application serve concurrent requests in a multithreaded environment , we should always make Stateless Beans.

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Java Spring Decoded
Java Spring Decoded

Written by Java Spring Decoded

All Articles related to java , spring , Backend Development and System Design.

No responses yet