Create the instance of Spring IoC Container

To create the instance of  Spring IoC container is very easy. 

There are two ways we can instantiate Spring IoC container.
The BeanFactory and ApplicationContext. These two are come under the most important packages are org.springframework.beans and org.springframework.context packages.
The BeanFactory - It provides the mechanism to manage the different kinds of beans(objects).
The ApplicationContext – ApplicationContext is the subclass of BeanFactory and adds more functionality such as easier integration with Springs AOP features, message resource handling (for use in internationalization)

Instantiating a IoC Container using the BeanFactory:

A BeanFactory is a interface which is represented by org.springframework.beans.factory.BeanFactory and it has multiple implementations. To instantiate the BeanFactory we have to load the resource object as

Resource resc=newFileSystemResource(beans.xml);

BeanFactory factory = new XmlBeanFactory(resc);

We can also use the following to instantiate the BeanFactory   
ClassPathResource resc = new ClassPathResource("beans.xml");

BeanFactory factory = new XmlBeanFactory(resc);

Instantiating a IoC Container using the ApplicationContext:

As we know that the ApplicationContext is also interface but it has some more advantages however it depends on situation. Here we use the ApplicationContext to instantiate the bean as :

ApplicationContext context=new ClassPathXmlApplicationContext("spingcontext.xml");
Since ApplicationContext is as BeanFactory so we can say that

BeanFactory fact=context


Post a Comment