My Peers always force me to use Spring for creating new instance of any object. As per as my understanding, Spring provides a platform to manage the Business objects more efficiently. Spring makes the architecture more modular and more flexible.
There are some instances where I feel not to use spring bean, and go for general method of creating new objects. But, my peers always forces to do it by giving excuses of performance improvement. I never find any article related to spring where the factor of performance mentioned.
Question :
If I am using spring beans for creating the object whether it improves the performance compare to creating new instances using “new” operator.
If you think rationally, how spring can be faster ? It’s wrapper over your code. It also goes through the default constructor/overloaded constructor which ever is applicable.
e.g.
import demo.dependency.injection.IAccount;
}
Bean configuration:
When you load the bean using application context, it will print
However, here we need to understand other than performance there are other advantages.
like
– DI
– Manageability
So i think performance reason is not justifiable.
Apart from that:
Spring IOC frameworks provide way to create instances using the spring bean configuration. However, it does not stop you to create instance using the new operator.
I would like to given an example where you may chose to use new instead of the spring bean.
Lets say in your web application you have singleton bean which has different methods for the business logic and in each operation you may need new object instances for operation (method level variable are thread safe).
Because if singleton bean has prototype bean then it will be invoked only once when singleton bean is invoked. Please go through this for complete understanding.
http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch04s04.html