Question

Both are actually ioc containers. But what is the actual difference between them? Which one is better to use?

Was it helpful?

Solution

ApplicationContext is derived from BeanFactory to provide added functionality to work in web application.

You can instantiate your spring container by just writing

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

or

ApplicationContext context = new ClassPathXmlApplicationContext{"spring_dao.xml","spring_service.xml};

You can use one or more xml file depending on your project requirement. As I am here using two xml files i.e. one for configuration details for service classes other for dao classes. Here ClassPathXmlApplicationContext is child of ApplicationContext.

For better understanding the difference your can check the http://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory site.

Choosing between BeanFactory and ApplicationContext is also depends how you want load your beans.

ApplicationContext is preferred unless you need to save resources, like on a mobile application.

OTHER TIPS

Both BeanFactory and Application are used to manage life cycle of beans, ApplicationContext can do all things that a BeanFactory does along with AOP,Event etc..

Unless until resources are crucial, go for ApplicationContext.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top