Spring singleton beans in different application contexts

We have a spring application (single threaded by design). We want to adapt it to multithreading. One idea was to create a parent thread and create different threads that would instantiate their own application context and run in parallel. (Memory and processor are not a problem right now). I'm not sure how singletones are implemented in spring. Does spring use a static link and return it, or use some kind of cache / map (which is non-static / non-single and contextual) where does it search? This will help me decide if config-xml should be changed. Any ideas please.

+3
source share
3 answers

Spring singletonbeans are created once for each application context. That is, if you create multiple application contexts from the same configuration, they will have different instances of singleton beans.

If you want them to share a single instance of a singleton bean, you can declare it in the context of the parent application and provide your multiple contexts to this parent context when they are created.

+5
source

Why do you need multiple application contexts to make an application multithreaded? Multiple threads can make excellent use of the same context.

Update: Look at the spring package

+2
source

, , beans . , , beans.

  • , .
  • , spring ? , , beans, , .
+1

Source: https://habr.com/ru/post/1732393/


All Articles