Spring Container

 

The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans.




 The object instantiated by spring is managed by spring container, when we call getBean() method. The whole life cycle is managed by spring container.

The object created by new keyword is no managed by spring container, and this object is managed by JVM.





More details about Application Context : 


ApplicationContext refers to spring XML configuration file before you make call to any getBean() method. It reads the spring XML, and it looks at all the beans available in spring XML and it actually creates them by itself. There is still no call to getBean() method. Without any call ApplicationContext during initialization itself initialize all the spring beans that we have configured in spring XML. Now after that when you have another object that actually doing  a getBean() then the ApplicationContext hadns over the bean that already been initialized. Creation of the bean happens when the ApplicationContext is initialized not by call of getBean() method. This is the default behaviour of the ApplicationContext(spring container). 



Note: By default, ApplicationContext implementations eagerly create and configure all singleton beans
as part of the initialization process. Y
ou 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 start-up.You can configure bean to be initialized only when getBean()  happens by making a bean  Prototype or by adding attribute lazy-init="true" to a particular bean.



You can also make all the beans lazy-initialized by adding default-lazy-init="true" attribute to <beans/> tag.












However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext creates the lazy-initialized bean at start-up  because it must satisfy the singleton's dependencies.






0 comments:

Post a Comment

 
Copyright (c) 2013 Java Discovery.