Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to guarantee a bean is instantiated only once by Spring

Tags:

java

spring

I would like to know how to tell Spring load a bean which I've defined in a context just once? Is this possible? How is this called and what do I need to do?

I'd basically like to make sure the class is never re-instantiated and that the same instance is re-used.

like image 651
carlspring Avatar asked Nov 15 '25 01:11

carlspring


1 Answers

All beans in spring are singletons by default so they will be instantiated just once. So you have this out of the box and need just to register bean in context by xml or Annotations.

like image 59
nkukhar Avatar answered Nov 17 '25 17:11

nkukhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!