First, Beanfactory
Beanfactory is the "heart" of Spring. It is the true face of the Spring IoC container. Spring uses Beanfactory to instantiate, configure, and manage beans.
Beanfactory: Is the core interface of the IOC container, which defines the basic functions of the IOC, and we see that it mainly defines the Getbean method. The Getbean method is a starting point for the IOC container to get the bean object and raise the dependency injection. The function of a method is to return a bean of a particular name.
Beanfactory are "hard-working people" that initialize beans and invoke their life-cycle methods. Note that beanfactory can only manage the life cycle of a singleton (Singleton) Bean. It cannot manage the life cycle of prototypes (prototype, non-singleton) beans. This is because the prototype Bean instance is passed to the client after it is created, and the container loses its reference to it.
Second, ApplicationContext
The Beanfactorty interface provides a configuration framework and basic functionality, but does not support spring's AOP features and Web applications. The ApplicationContext interface, as a derivation of the beanfactory, provides all beanfactory functionality. And the ApplicationContext is also extended functionally, with the following features compared to Beanfactorty,applicationcontext:
(1) Messagesource, providing international message access
(2) resource access, such as URLs and files
(3) Event propagation characteristics, i.e. support for AOP features
(4) Loading multiple (inherited relationships) contexts so that each context is focused on a specific level, such as the web layer of the application
ApplicationContext: is another important interface of the IOC container, which inherits the basic functions of beanfactory, and also inherits the advanced functions of the container, such as: Messagesource (internationalized Resource interface), Resourceloader (Resource Loading interface), Applicationeventpublisher (Application Event Publishing interface), and so on.
Three or two differences
1.BeanFactroy uses lazy loading as a way to inject beans, which is to instantiate a bean only when it is used (called Getbean ()), so that we cannot discover some of the existing spring configuration problems. ApplicationContext, on the other hand, creates all the beans at once when the container is started. This way, when the container starts, we can discover the configuration errors that exist in spring. The only disadvantage relative to the basic beanfactory,applicationcontext is the memory footprint. When the application configuration bean is large, the program starts slowly.
2.BeanFactory and ApplicationContext both support the use of beanpostprocessor, beanfactorypostprocessor, but the difference between the two is: Beanfactory need to manually register, The ApplicationContext is automatically registered. (ApplicationContext has added some better-used features than beanfactory.) And many functions of beanfactory need to be implemented programmatically and ApplicationContext can be implemented by configuration. such as post-processing beans, ApplicationContext directly configured in the configuration file can be beanfactory this to be displayed in the code to be written out to be recognized by the container. )
3.beanFactory is primarily faced with the spring framework infrastructure, facing spring itself. The Applicationcontex is mainly used by developers with spring. The basic use of Applicationcontex is not beanfactory.
Iv. Summary
Role:
1. The beanfactory is responsible for reading the bean configuration document, managing the bean's loading, instantiating, maintaining the dependency between the beans, and responsible for the bean's declaration cycle.
2. ApplicationContext provides more complete framework functionality in addition to the functionality provided by the beanfactory above:
A. Internationalization support
B. Resource access: Resource rs = ctx. GetResource ("Classpath:config.properties"), "File:c:/config.properties"
C. Event delivery: By implementing the Applicationcontextaware interface
3. Commonly used to get ApplicationContext
Filesystemxmlapplicationcontext: Created from a file system or URL-specified XML configuration file with an array of configuration file names or file names
Classpathxmlapplicationcontext: Created from the classpath XML configuration file, you can read the configuration file from the jar package
Xmlwebapplicationcontext: Reading a configuration file from the Web application's root directory requires a configuration in XML. config, which can be configured with a listener or servlet to implement
< Listener > < Listener-class >org.springframework.web.context.ContextLoaderListener</listener-class ></listener>
Or
<servlet><Servlet-name>Context</Servlet-name><Servlet-class>Org.springframework.web.context.ContextLoaderServlet</Servlet-class><Load-on-startup>1</Load-on-startup></servlet>
Either way, the default profile is Web-inf/applicationcontext.xml, or you can use Context-param to specify the configuration file
< Context-param > < Param-name >contextconfiglocation</param-name><Param-value >/web-inf/myapplicationcontext.xml</param-value> </ Context-param >
The Beanfactory and ApplicationContext of Spring Series