Are AbstractApplicationContext and it's descendants (File-, Classpath-) thread-safe? Can I use one instance to get beans from multiple threads? What's the strategy of using it in standalone multithreaded apps?
Are AbstractApplicationContext and it's descendants (File-, Classpath-) thread-safe? Can I use one instance to get beans from multiple threads? What's the strategy of using it in standalone multithreaded apps?
Yes, the ApplicationContext descendants are thread-safe.
When using the context in a stand-alone application you can use one of the BeanFactoryLocators or instantiate the ApplicationContext yourself (I suggest the former).
Have a look at the BeanFactoryLocator JavaDoc and its subclasses.
alef
Hi Alef,
After extensive googling on this topic, this was the only reference or assertion about spring's thread safety.
Could please explain in more detail how Spring guarantees the thread safety of the application context?
In particular, how does an application context guarantee that two threads cannot execute the same methods on the same bean at the same time?
Thanks!
Sorry, but you have completely mixed 2 concepts -
thread safety of application context (which is just normal Java object with its own methods) and thread-safety of the beans created by the context as specified by configuration.
Thread safety of application context means that methods of application context object may be concurrently called from many threads.
It is achieved by design and coding of application context itself.
Thread safety of the beans created by context - completely different matter,
the only garantee that Spring gives is that bean is completely configured according to config info before is given out by context. Spring do not manage access to created beans in any way or attempts any synchronization efforts on them. It is completely up to you ensure that your beans are thread-safe.
You may achive it by creating beans without mutable state (typical for DAOs and services) using non-shared beans (like prototype beans) or providing proper synchronization on your own.
BTW, thread safety of the Spring was discussed many times in these forums as well as in many other forums, articles and blogs all over the Internet. I really do not understand why you have been unable to find links.
Regards,
Oleksandr