Is a prototype bean created faster by the spring container than a singleton bean? How much if yes?

Maybe some background for the question. Assume that we have the context definition which contains a lot of bean definitions. And the application is a 'short running task' I wonder if I can speed up creation of the context by defining all beans as prototypes - because in this case it doesn't matter if they are singletons or not.

有帮助吗?

解决方案

The two approaches are completely different in the amount of beans created.

  • When the bean has a scope="prototype" this means a new bean instance will be created everytime you ask for a bean with the corresponding id.

  • When the bean doesn't have a scope attribute set, this means a single instance will be created when the context is loaded for the first time and will be shared.

I believe the time consumed for creation of prototype and singleton beans is (if not the same) very close.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top