문제

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