Wednesday 15 July 2015

BeanFactory vs ApplicationContext

org.springframework.beans.factory.BeanFactory and 
org.springframework.context.ApplicationContext interfaces acts as the IoC container.

The ApplicationContext interface is built on top of the BeanFactory interface.

It adds some extra functionality than BeanFactory such as simple integration with Spring's AOP, message resource handling (for I18N), event propagation, application layer specific context (e.g. WebApplicationContext) for web application. So it is better to use ApplicationContext than BeanFactory.

Using BeanFactory

The XmlBeanFactory is the implementation class for the BeanFactory interface.


Resource resource = new ClassPathResource("applicationContext.xml");  
BeanFactory factory = new XmlBeanFactory(resource); 
Employee s=(Employee) factory.getBean("e");


The constructor of XmlBeanFactory class receives the Resource object so we need to pass the resource object to create the object of BeanFactory.

Using ApplicationContext

The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface.


ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");  
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...