ApplicationContext
The ApplicationContext interface makes the Beanfactory sub-interface, which represents the context of an application.
ApplicationContext extends many of the features used, such as
- Life cycle management of beans
- Framework Event System
- Internationalization support, etc.
At the same time, the ApplicationContext interface extends the functionality of Beanfactory by inheriting other interfaces:
- Messagesource ——— provides international access for applications
- Resourceloader ——— provide access support for resources such as URLs and file systems
- Applicationeventpublisher ——— introduces event mechanisms, including startup events, shutdown events, and so on, allowing the container to provide support for application events in context.
The main implementation classes of ApplicationContext are
- Classpathxmlapplicationcontext (The IOC configuration file is loaded from the classpath);
- Filesystemxmlapplicationcontext (The IOC configuration file is loaded from the file system).
PackageDemo02;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by Richard on 2017/7/24.*/ Public classApplicationcontextdemo { Public Static voidMain (string[] args) {Classpathxmlapplicationcontext context=NewClasspathxmlapplicationcontext ("Demo02/bean.xml"); Book Book= (book) Context.getbean ("book")); System.out.println (Book.getname ()); System.out.println (Book.getauthor ()); System.out.println (Book.getpublisher ()); System.out.println (Book.getprice ());
Context.close (); }}
Because the Classpathxmlapplicationcontext class implements the Java.io.Closeable interface, it is necessary to call the close () method to close after the use is complete.
ApplicationContext && beanfactory Initialization differences
Beanfactory initializes the container so that the bean is not instantiated, and the target bean is not instantiated until the first time a bean is accessed.
ApplicationContext instantiates all single-instance beans when the context is initialized.
Bean's life cycle
Spring Learning Note (iii)--IOC container (ApplicationContext)