Reputation: 355
1. BeanFactory fac=new ClassPathXmlApplicationContext("Spring-Config.xml");
2. Resource res=new Classpathresource("Spring-Config.xml");
BeanFactory fac=new XmlBeanFactory(res);
(I found 2nd type only i can specify a parent Configration file for using
3. ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
I have 3 ways of loading beanfactory and I want know what is the difference between them. How can I define the parent configuration file in all those methods if it is possible?
Upvotes: 2
Views: 7263
Reputation: 3789
BeanFactory and ApplicationContext both are ways to get beans from your spring IOC container but still there are some difference.
BeanFactory is the actual container which instantiates, configures, and manages a number of bean's. These beans are typically collaborate with one another, and thus have dependencies between themselves. These dependencies are reflected in the configuration data used by the BeanFactory.
BeanFactory and ApplicationContext both are Java interfaces and ApplicationContext extends BeanFactory. Both of them are configuration using XML configuration files. In short BeanFactory provides basic Inversion of control(IoC) and Dependency Injection (DI) features while ApplicationContext provides advanced features.
A BeanFactory is represented by the interface "org.springframework.beans.factory" Where BeanFactory, for which there are multiple implementations.
ClassPathResource resource= new ClassPathResource("appConfig.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);
DIFFERENCE
BeanFactory instantiate bean when you call getBean() method while ApplicationContext instantiate Singleton bean when container is started, It doesn't wait for getBean() to be called.
BeanFactory doesn't provide support for internationalization but ApplicationContext provides support for it.
Another difference between BeanFactory vs ApplicationContext is ability to publish event to beans that are registered as listener.
One of the popular implementation of BeanFactory interface is XMLBeanFactory while one of the popular implementation of ApplicationContext interface is ClassPathXmlApplicationContext.
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
BeanFactory by default its support Lazy loading and ApplicationContext by default support Aggresive loading.
Upvotes: 0
Reputation: 1350
The ApplicationContext is derived from BeanFactory interface, so it has all the functions that BeanFactory has and also has extra functions, below part is in Spring official website: MessageSource, providing access to messages in i18n-style. Access to resources, such as URLs and files. Event propagation to beans implementing the ApplicationListener interface. Loading of multiple (hierarchical) contexts, allowing each to be focused on one particular layer, for example the web layer of an application.
As you said that some code uses BeanFactory and some uses ApplicationContext, it's actually no more different, but 1 thing, because of the extra functions of ApplicationContext,it will be more heavy and can work with transaction and aop, it will be very good to used in the container environment, such as Tomcat and others. You can find more in here, especially sec 3.8.1. BeanFactory or ApplicationContext?: http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory
Upvotes: 2
Reputation: 2711
The BeanFactory hierarchy is shown in an image here.
Please note that these are interfaces and an interface can have many - many - implementations and hence corresponding different ways of getting a bean.
And here is a difference listing BeanFactory Vs ApplicationContext
Upvotes: 1