我是Spring框架的新手,我一直在摆弄它,并把一些样本应用放在一起,目的是评估Spring MVC在即将到来的公司项目中的使用。到目前为止,我真的很喜欢Spring MVC,它看起来很容易使用,并鼓励你编写对单元测试非常友好的类。
作为练习,我正在为我的一个示例/测试项目编写一个主方法。我不清楚的一件事是BeanFactory和ApplicationContext之间的确切区别——哪个适合在哪些条件下使用?
我知道ApplicationContext扩展了BeanFactory,但是如果我只是编写一个简单的主方法,我是否需要ApplicationContext提供的额外功能呢?ApplicationContext究竟提供了什么样的额外功能?
除了回答“我应该在main()方法中使用哪个”之外,关于在这样的场景中应该使用哪个实现,是否有任何标准或指南?我的main()方法是否应该被编写成依赖于bean/应用程序配置的XML格式——这是一个安全的假设吗,还是我将用户锁定到某个特定的东西?
这个答案在web环境中会改变吗——如果我的任何类需要了解Spring,它们更可能需要ApplicationContext吗?
谢谢你的帮助。我知道很多问题都可以在参考手册中找到答案,但如果没有仔细阅读手册,我很难找到这两个接口的清晰分解以及各自的优缺点。
在实时场景中,Spring IOC核心容器(BeanFactory)和高级J2EE容器(ApplicationContext)之间的区别如下。
BeanFactory will create objects for the beans (i.e., for POJO classes) mentioned in the spring.xml file (<bean></bean>) only when you call the .getBean() method, but whereas ApplicationContext creates the objects for all the beans (<bean></bean> if its scope is not explicitly mentioned as "Prototype") configured in the spring.xml while loading the spring.xml file itself.
BeanFactory: (Lazy container because it creates the objects for the beans only when you explicitly call from the user/main class)
/*
* Using core Container - Lazy container - Because it creates the bean objects On-Demand
*/
//creating a resource
Resource r = (Resource) new ClassPathResource("com.spring.resources/spring.xml");
//creating BeanFactory
BeanFactory factory=new XmlBeanFactory(r);
//Getting the bean for the POJO class "HelloWorld.java"
HelloWorld worldObj1 = (HelloWorld) factory.getBean("test");
ApplicationContext: (Eager container because of creating the objects of all singleton beans while loading the spring.xml file itself)
ApplicationContext context = new ClassPathXmlApplicationContext("com/ioc/constructorDI/resources/spring.xml");
Technically, using ApplicationContext is recommended because in real-time applications, the bean objects will be created while the application is getting started in the server itself. This reduces the response time for the user request as the objects are already available to respond.
Spring提供两种IOC容器,一种是XMLBeanFactory,另一种是ApplicationContext。
|
BeanFactory |
ApplicationContext |
Annotation support |
No |
Yes |
BeanPostProcessor Registration |
Manual |
Automatic |
Implementation |
XMLBeanFactory |
ClassPath/FileSystem/WebXmlApplicationContext |
Internationalization |
No |
Yes |
Enterprise services |
No |
Yes |
ApplicationEvent publication |
No |
Yes |
通过完整路径加载的FileSystemXmlApplicationContext bean。
通过CLASSPATH加载的bean
XMLWebApplicationContext和AnnotationConfigWebApplicationContext bean通过web应用程序上下文加载。
从基于Annotation的配置中加载Spring bean。
例子:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeansConfiguration.class);
ApplicationContext是由web.xml中定义的ContextLoaderListener或ContextLoaderServlet和struts-config.xml中定义的ContextLoaderPlugin初始化的容器。
注意:XmlBeanFactory从Spring 3.1开始已弃用,取而代之的是DefaultListableBeanFactory和XmlBeanDefinitionReader。
BeanFactory和ApplicationContext都是从spring IOC容器中获取bean的方法,但仍有一些区别。
BeanFactory是实例化、配置和管理许多bean的实际容器。这些bean通常彼此协作,因此它们之间存在依赖关系。这些依赖关系反映在BeanFactory使用的配置数据中。
BeanFactory和ApplicationContext都是Java接口,而ApplicationContext扩展了BeanFactory。它们都是使用XML配置文件进行配置的。简而言之,BeanFactory提供了基本的控制反转(IoC)和依赖注入(DI)特性,而ApplicationContext提供了高级特性。
BeanFactory由接口“org.springframework.beans”表示。其中BeanFactory有多个实现。
ClassPathResource resource = new ClassPathResource("appConfig.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);
区别
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.