AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-spring: What is the difference between BeanFactory and ApplicationContext?

Please explain the difference between BeanFactory and ApplicationContext in Spring.

java x 210
spring x 24
Posted On : 2016-06-03 22:17:27.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221945
up-rate
4
down-rate

Answers


ApplicationContext is the preferred way of using spring because of functionality provided by it. ApplicationContext extends BeanFactory. Both of them are configuration using XML configuration file. In short BeanFactory provides basic IOC and DI features while ApplicationContext provides advanced features. Apart from these, Here are few more difference between BeanFactory and ApplicationContext which is mostly based upon features supported by them.

1) BeanFactory doesn´t provide support for internationalization i.e. i18n but ApplicationContext provides support for it.

2) Another difference between BeanFactory vs ApplicationContext is ability to publish event to beans that are registered as listener.

3) One of the popular implementation of BeanFactory interface is XMLBeanFactory while one of the popular implementation of ApplicationContext interface is ClassPathXmlApplicationContext. In Java web application we use WebApplicationContext which extends ApplicationContext interface and adds getServletContext method.

4) If you are using auto wiring and using BeanFactory than you need to register AutoWiredBeanPostProcessor using API which you can configure in XML if you are using ApplicationContext. In summary BeanFactory is OK for testing and non production use but ApplicationContext is more feature rich container implementation and should be favored over BeanFactory


These were some worth noting difference between BeanFactory and ApplicationContext in Spring framework. In most practical cases you will be using ApplicationContext but knowing about BeanFactory is important to understand fundamental concept of spring framework. I mostly use XML configuration file and ClassPathXmlApplicationContext to quickly run any Spring based Java program from Eclipse by using following snippet of code :

public static void main(String args[]){
ApplicationContext ctx =new ClassPathXmlApplicationContext("beans.xml");
Hello hello =(Hello) ctx.getBean("hello");
hello.sayHello("John");
}

here beans.xml is your spring configuration file and "hello" is a bean defined in that spring configuration file. Here we have used ClassPathXmlApplicationContext which is an implementation of ApplicationContext interface in Spring.

ApplicationContextBeanFactory
Here we can have more than one config files possible In this only one config file or .xml file
Application contexts can publish events to beans that are registered as listeners Doesn't support.
Support internationalization (I18N) messages It's not
Support application life-cycle events, and validation. Doesn't support.
Supports  many enterprise services such JNDI access, EJB integration, remoting Doesn't support.

Posted On : 2016-06-03 22:48:34
Satisfied : 2 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129026006
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in