Does IOC container makes object's when project is deployed?

Refresh

April 2019

Views

115 time

1

How IOC container helps maintaing objects by creating once and injecting when required???

2 answers

0

It depends how you have configured the specific dependency, you can have singleton, per request, http etc lifecycles

1

Read the spring reference about Bean Scopes and about Lazy Initialization:

By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.

In XML, this behavior is controlled by the lazy-init attribute on the element; for example:

<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>

<bean name="not.lazy" class="com.foo.AnotherBean"/>