简体   繁体   中英

How does Singleton work in Spring?

Spring default setting is Singleton for beans. So does that mean when 100 users access the same the site (service or bean), do those 100 sessions share the single instance of that service bean in thread manner or will 100 beans be created and each session has its own bean. If it is a latter, then how does Singleton pattern apply to it? Can somebody please reply with a possible code example with which we can see for ourselves.

Singleton means that a single instance will be created of a given class for the lifetime of the application/process. So if 100 users access the same bean, all of them will get the same instance.

It's the former. You get one object only with singleton. You can get one object per session, but that is a different keyword I believe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM