问题描述
1. BeanFactory fac=new ClassPathXmlApplicationContext("Spring-Config.xml");
2. Resource res=new Classpathresource("Spring-Config.xml");
BeanFactory fac=new XmlBeanFactory(res);
(我发现只有第二种类型我可以使用
指定一个父Configration文件
(I found 2nd type only i can specify a parent Configration file for using
3. ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
我有3种加载beanfactory的方法,我想知道它们之间有什么区别。
如何在所有方面定义父配置文件那些方法是否可能?
I have 3 ways of loading beanfactory and I want know what is the difference between them.How can I define the parent configuration file in all those methods if it is possible?
推荐答案
ApplicationContext派生自BeanFactory接口,因此它具有BeanFactory所具有的所有功能,还有额外的功能,下面是Spring官方网站:
MessageSource,提供对i18n风格的消息的访问。
访问资源,例如URL和文件。
事件传播到实现ApplicationListener接口的bean。
加载多个(分层)上下文,允许每个上下文集中于一个特定的上下文图层,例如应用程序的Web图层。
The ApplicationContext is derived from BeanFactory interface, so it has all the functions that BeanFactory has and also has extra functions, below part is in Spring official website: MessageSource, providing access to messages in i18n-style. Access to resources, such as URLs and files. Event propagation to beans implementing the ApplicationListener interface. Loading of multiple (hierarchical) contexts, allowing each to be focused on one particular layer, for example the web layer of an application.
正如您所说的一些代码使用BeanFactory而某些代码使用ApplicationContext,它实际上没有什么不同,但是1,因为ApplicationContext的额外功能,它会更加繁重,可以与事务和aop 一起使用,在容器环境中使用它会非常好,例如Tomcat等。
你可以在这里找到更多,尤其是第3.8.1节。 BeanFactory或ApplicationContext?:
As you said that some code uses BeanFactory and some uses ApplicationContext, it's actually no more different, but 1 thing, because of the extra functions of ApplicationContext,it will be more heavy and can work with transaction and aop, it will be very good to used in the container environment, such as Tomcat and others.You can find more in here, especially sec 3.8.1. BeanFactory or ApplicationContext?: http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory
这篇关于创建beanfactory对象有哪些不同的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!