In short,BeanFactoryProvides the preparation framework function, whileApplicationContextThen, more functions are added to support the core content of the enterprise.ApplicationContextCompletelyBeanFactoryThereforeBeanFactoryThe capabilities and behaviors are also applicableApplicationContext.
ApplicationContext includes all BeanFactory functions. It is generally recommended to give priority to BeanFactory unless there are some restrictions, such as when the byte length has a great impact on the memory (Applet ). Then, for most "typical" enterprise applications and systems, ApplicationContext is what you need. Spring2.0 and later versions use a large number of link linkend = "beans-factory-extension-bpp"> BeanPostProcessor extensions (to facilitate application proxy and other functions ), if you choose BeanFactory, you cannot use a considerable number of supported functions, such as transactions and AOP, which may cause confusion because the configuration is not correct.
The function matrix below lists the features provided by BeanFactory and the features provided by ApplicationContext (including their implementations ). (The next section describes more advanced ApplicationContext functions than BeanFactory .)
Table 3.5. Feature Matrix Feature table
BeanFactory ApplicationContext
Bean instantiation/Assembly Yes
Automatic BeanPostProcessor Registration No Yes
Automatic beanfactorypostprocessor Registration No Yes
Convenient messagesource access (i18n) No Yes
Applicationevent sends no Yes
Use a beanfactory implementation xmlbeanfactory to load the configuration file
Import org. springframework. beans. factory. xml. xmlBeanFactory; <br/> import org. springframework. core. io. fileSystemResource; </p> <p> public class XmlConfigWithBeanFactory {</p> <p> public static void main (String [] args) {<br/> XmlBeanFactory factory = new XmlBeanFactory (new FileSystemResource (<br/> "build/beans. xml "); </p> <p >}< br/>}
Use applicationcontext to load the configuration file
Public class xmlconfigwithapplication {</P> <p> Public static void main (string [] ARGs) {<br/> applicationcontext application = new classpathxmlapplicationcontext (beans. XML "); <br/> application. getbean ("beanname"); <br/>}< br/>}
Compared with BeanFacotry, ApplicationContext provides more extension functions, but the main difference is that the latter delays loading. If a Bean property is not injected, after BeanFacotry is loaded, an exception is thrown only when the getBean method is called for the first time. ApplicationContext is a test in initialization, which helps to check whether the dependent attributes are injected. Therefore, we usually choose ApplicationContext.
BeanFactory
BeanFactory is an Interface, which is the core of Spring. It only provides some basic functions.
ApplicaionContext
It is also an interface, which is inherited from BeanFactory, so it has all the features of BeanFactory. However, it also obtains some features from other classes or interfaces. Its signature is as follows:
Public interface applicationcontext extends listablebeanfactory, hierarchicalbeanfactory, <br/> messagesource, applicationeventpublisher, resourcepatternresolver <br/>
ListableBeanFactory: Mainly used to list the xml part of the bean. How many beans or something?
HierarchicalBeanFactory: A HierarchicalBeanFactory can correctly locate the entire BeanFactory
Other BeanFactory.
MessageSource: Mainly used i18. A Beanfactory that implements MessageSource also has an international function.
ApplicationEventPublisher: listens to events. Event support.
ApplicationContext and BeanFactory are both loaded by encoding. For Web applications, Spring provides a configurable ApplicationContext loading mechanism, that is, WebContext.
Currently, the loader has two options: ContextLoaderListener and ContextLoaderServlet. Both are fully functional.
Equivalent: it is implemented based on the Listener interface introduced in Servlet2.3, while the other is implemented based on the Servlet interface.
You can select the target Web Container based on the actual situation.