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

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top