0

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.

Łukasz Rzeszotarski
  • 5,791
  • 6
  • 37
  • 68
  • 1
    I might suggest that they take the same *amount* of time to create, but in general, a `singleton` scope bean will be *fetched* each time it is requested from the application context, while a `prototype` bean will need to be *created* each time it is requested from the application context. – nicholas.hauschild Oct 23 '13 at 14:10

1 Answers1

1

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.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147