我是Spring框架的新手,我一直在摆弄它,并把一些样本应用放在一起,目的是评估Spring MVC在即将到来的公司项目中的使用。到目前为止,我真的很喜欢Spring MVC,它看起来很容易使用,并鼓励你编写对单元测试非常友好的类。

作为练习,我正在为我的一个示例/测试项目编写一个主方法。我不清楚的一件事是BeanFactory和ApplicationContext之间的确切区别——哪个适合在哪些条件下使用?

我知道ApplicationContext扩展了BeanFactory,但是如果我只是编写一个简单的主方法,我是否需要ApplicationContext提供的额外功能呢?ApplicationContext究竟提供了什么样的额外功能?

除了回答“我应该在main()方法中使用哪个”之外,关于在这样的场景中应该使用哪个实现,是否有任何标准或指南?我的main()方法是否应该被编写成依赖于bean/应用程序配置的XML格式——这是一个安全的假设吗,还是我将用户锁定到某个特定的东西?

这个答案在web环境中会改变吗——如果我的任何类需要了解Spring,它们更可能需要ApplicationContext吗?

谢谢你的帮助。我知道很多问题都可以在参考手册中找到答案,但如果没有仔细阅读手册,我很难找到这两个接口的清晰分解以及各自的优缺点。


当前回答

Bean工厂的特性矩阵vs来自spring文档的应用程序上下文

beanfactory和ApplicationContext功能的截图

其他回答

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。

a. bean工厂和应用程序上下文之间的一个区别是前者只在调用getBean()方法时实例化bean,而ApplicationContext在容器启动时实例化单例bean,它不等待getBean被调用。

b。

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

or

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

您可以根据项目需求使用一个或多个xml文件。我在这里使用了两个xml文件,即一个用于服务类的配置细节,另一个用于dao类。这里ClassPathXmlApplicationContext是ApplicationContext的子。

c. BeanFactory Container是基本容器,它只能创建对象和注入依赖项。但我们不能附加其他服务,如安全、事务、消息等,以提供我们必须使用ApplicationContext容器的所有服务。

d. BeanFactory不提供国际化支持,即i18n,但ApplicationContext提供了国际化支持。

e. BeanFactory容器不支持自动扫描(支持基于注释的依赖注入)特性,但ApplicationContext容器支持。

f.直到请求时间,Beanfactory容器才会创建bean对象。这意味着Beanfactory容器会懒惰地装载bean。而ApplicationContext容器只在加载时创建单例bean的对象。这意味着有早期装载。

Beanfactory容器只支持bean的两个作用域(单例和原型)。但是ApplicationContext容器支持所有的bean范围。

我需要解释BeanFactory & ApplicationContext。

BeanFactory: BeanFactory is root interface for accessing the SpringBean Container.There is basic client view of a bean container. That interface is implemented by the object class that holds the number of beans definitions, and each uniquely identify by the String name Depending the Bean definition the factory will return the instance that instance may be the instance of contained object or a single shared instance. Which type of instance will be return depends of bean factory configuration. Normally Bean factory will load the all the all the beans definition, which stored in the configuration source like XML...etc.

BeanFactory是一个为依赖注入提供基本支持的最简单的容器

应用程序上下文 应用程序上下文是spring应用程序中的一个中心接口,它向应用程序提供配置信息。它实现了Bean工厂接口。

应用程序上下文是一个高级容器,它增加了高级级别的企业特定功能,例如从属性文件....解析文本消息的能力等

一个ApplicationContext提供:

Bean factory methods for accessing application components. Inherited from ListableBeanFactory. The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface. The ability to publish events to registered listeners. Inherited from the ApplicationEventPublisher interface. The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface. Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet. In addition to standard BeanFactory lifecycle capabilities, ApplicationContext implementations detect and invoke ApplicationContextAware beans as well as ResourceLoaderAware, ApplicationEventPublisherAware and MessageSourceAware beans.

我的六分钱:

BeanFactory and ApplicationContext both are interfaces where ApplicationContext extends BeanFactory. So ApplicationContext is a child interface and BeanFactory is the parent interface. In other words, ApplicationContext has all the features of BeanFactory and it has some additional features that are not present in BeanFactory. BeanFactory loads beans on-demand that is Lazy Loading whereas ApplicationContext loads all beans at startup that is Eager Loading. BeanFactory does not support annotations whereas ApplicationContext supports annotations. ApplicationContext has additional features that are not present in BeanFactory: Internationalization, Event Publishing, AOP features. BeanFactory only supports two scopes — Singleton and Prototype whereas ApplicationContext supports all bean scopes. BeanFactory does not register BeanFactoryPostProcessor and BeanPostProcessor automatically whereas ApplicationContext automatically registers them.

ApplicationContext是比BeanFactory更可取的方式 在新的Spring版本中,BeanFactory被ApplicationContext取代。但是仍然存在用于向后兼容的BeanFactory ApplicationContext扩展了BeanFactory,并具有以下优点 它支持文本消息的国际化 它支持将事件发布到已注册的侦听器 访问资源,如url和文件