Pages

Monday, 29 July 2013

Spring Container & Bean Life Cycle

Basic Spring Container

Spring Container is core part of Spring frameworks.Spring Container provide or uses the IOC (Inversion of Control) for the manage the Component means Beans that made up your applications.

There are two types of Spring container in Spring framework.

1). First One defined by org.springframeworks.bean.factory.BeanFactory interface  this the most basic type   of Spring Container provide the basic Support for the instantiation and dispatch the bean and inject the       dependencies.

2). Second One defined by org.springframeworks.context.ApplicationContext interface this build from the using of the BeanFactory wrapper and it's provide the application frameworks services like the resolve the textual message from the properties file and we can implement the events listener on the application context.

Let's see the Spring Containers
1). BeanFactory :- It is the implementation of the factory design pattern and it's responsible for the create and dispance the bean. The Impementation of BeanFactory in Spring is the org.springframework.beans.factory.xml.XmlBeanFactory it load the bean from the specify in XML file.For that you have to pass the InputStream Object in the Constructor and in the InputStream you have to pass the XML file. like
BeanFactory beanFactory = new XMLBeanFactory(new FileInputStream("beans.xml"));
This is the way to read the bean definition from the XML file. All the beans are loaded lazy so it's doesn't instantiated  immediately themselves but it's loaded when the first request made for the bean using the getBean("BeanName").this method is instatiated the bean instance and set the bean properteies using the dependencies injection.
        MyBean myBean = (MyBean) beanFactory.getBean("myBean");
This is the most basic thing about the Spring Container.
2). ApplicationContext:- This is the most advanced the Spring Container. This container is implemented by the three classes.
    
1). ClassPathXMLApplicationContext:- Load the Context definition from the XML file from the available class path.
2). FileSystemXMLApplicationContext:- Load the Context definition from the XML File from the FileSystem.
3). XMLWebApplicationContext:- Load the Context definition from the XML file from the Web Application.
Let's see what is difference between the FileSystemXMLApplicationContext and ClassPathXMLApplicationContext and how we are use it for the load the beans.xml
ApplicationContext applicationContext = new FileSystemXMLApplicationContext("C:/beans.xml");
Here we load the context defintion from the Absolute path of file system.
ApplicationContext applicationContext = new ClassPathXMLApplicationContext("beans.xml");
it look in to the classpath and read the context definition file.
Now the Next we see the difference between the BeanFactory and ApplicationContext.As we discussed early how to access  the bean through BeanFactory that way use in the ApplicationContext because it's has the BeanFactory wrapper. But  it provide the some addition functionality which are not provide by the BeanFactory.

1). Provide the Support for the textual message resolving and Internationalization.
2). Provide the Generics way for loading the resouces like the images.
3). Registered the event Listener on the ApplicationContext.
4). In the BeanFactory the beans created when the getBean(myBean) called. But In ApplicationContext loaded the all the Singleton beans at the context start up. So we don't need to wait for bean creation.
Spring Bean Life Cycle. In this Section we will see what is life cycle of Bean when it created and instantiated by BeanFactory & ApplicationContext and what 's different between them. Step of Bean Life cycle when created and instantiated by the BeanFactory. 1). First Spring Container find the Bean defintion and create & Instantiated it 2). Using the getBean method we injecting the all the properties dependencies specified in the bean definition. 3). If the Bean Implements the BeanNameAware Interface ,the factory call the setBeanName() method passing the bean id. 4). If the bean implements the BeanFactoryAware Interface, the factory call the setBeanFactory() method passing the instance itself. 5). If the BeanPostProcessor associated with the bean then PostProcessBeforeInitalization() method called. 6). If bean specify the init-method then it will called. 7). At Last the method of BeanPostProcessor interface PostProcessAfterInitailzation() called.
At this point the bean available in bean factory and application for use. There are two ways to remove the bean from the bean factory. 1). If the bean implements the DisponsableBean interface , the destory() method called. 2). If bean specify the destory-method then it will called. Step of Bean Life cycle when created and instatiated by the ApplicationContext. It follow the same step From 1) to 4) after that there is change in step 5 in this case.
Add caption
5). If a bean Implement the ApplicationContextAware Interface then it called the setApplicationContext() method called. Rest of Step as same as the previous one.
Reference from the Spring in Action Book

No comments:

Post a Comment