In designing a new, very performance-sensitive part of our systems we investigated the runtime performance of retrieving beans (singleton and prototype) from a Spring bean factory versus creating them via a hand-coded factory. For the Spring code we also measured any additional overhead of auto-wiring beans and doing dependency checks on beans.
The test repeatedly retrieves 5 beans which are the roots of a highly interconnected object graph (comprising 4 other beans) from a Spring bean factory. In the prototype tests each bean in that graph is a Spring prototype bean, i.e. a new instance is create whenever a bean is needed from the bean factory. In the singleton tests each bean in that graph is a Spring singleton and so the same instance is returned every time a bean is retrieved. By comparison, the hand-coded factory always creates each object in that graph and hence behaves identical to the Spring prototype test.
The results give the time in nanoseconds for retrieving a bean (which is the root of the object graph) from the factory:
|Bean retrieval / nanoseconds||Spring 2.0.5||Spring 2.0.4||Spring 2.0.3||Spring 2.0.2|
|Spring, prototype, autowire=byType, depend check||442365||500050||945551||958805|
|Spring, prototype, autowire=byName,|
|Spring, prototype, no autowire, depend check||172412||173539||624171||639640|
|Spring, prototype, no autowire,no depend check||162300||162950||586144||600320|
|Spring, singleton, autowire=byType, depend check||608||841||1075||1134|
|Spring, singleton, autowire=byName,depend check||710||840||1069||1132|
|Spring, singleton, no autowire,depend check||609||842||1097||1161|
|Spring, singleton, no autowire, no depend check||614||837||1102||1135|
|No Spring (hand-coded factory),always create (prototype)||284||284||288||292|
The code we used to perform these measurements and all results are available here.