Through its factory, Spring's IoC container manages the objects that it is configured to instantiate. Spring's management of the container objects adds flexibility and control to your application, and provides a central place of configuration management for your Plain Old Java Objects.
For example, through Spring IoC you can configure the number of instances of the component –
whether the component is a singleton or not – and at what point the component is created and destroyed in memory.
In Spring, the initialization of a bean by the framework is exactly equivalent to using the new
keyword to instantiate an object in Java code.
Once the framework has instantiated the object, it manages the scope of the bean, based on its configuration.
Because the IoC container is managing the beans, JNDI lookups that are typical in Java EE containers are no longer required, leaving your code container-agnostic and easier to unit test both inside and outside of the framework. And while you are coding to interfaces as part of good OO practice, Spring allows you to manage what implementations are used by leveraging dependency injection, resulting in cleaner, decoupled components.
The IoC container can also be configured to receive instantiation and destruction callback events for a particular bean. Certain components such as a database connection pool obviously need to be initialized and destroyed when the application is shutdown. Instead of using your custom code, Spring can manage these lifecycle events.