Class Structure:
public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver
Definition
Applicationcontext: provides application context information. The program is read-only, but can be reloaded (if supported ).
The core of the context package isApplicationContext
Interface. It consistsBeanFactory
And providesBeanFactory
All functions. In order to work in a more framework-oriented way and to layer and implement context inheritance,ApplicationContext
Provides the following features:
It provides support for beanfactory layering. The so-called layering is the concept of parentbeanfactory. Inherits the hierarchicalbeanfactory Interface
Enumerate all bean instances and access Bean factory methods by application components. Inherited from
ListableBeanFactory
.
It can load file resources in a common way (such as application *. XML. InheritedResourceLoader
The sub-interface resourcepatternresolver of the API.
Event support. InheritedApplicationEventPublisher
Interface.
International support. InheritedMessageSource
Interface.Applicationcontext includes all functions of beanfactory. Therefore, we recommend that you use applicationcontext first. Beanfactory is considered only for memory-critical applications.
Obtaining Method
In many cases, applicationcontext allows you to operate containers in declarative mode without manual creation. You can use the contextloader Support class to automatically create applicationcontext when a web application starts. Of course, you can also use programming to create applicationcontext.
Declarative:
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:spring/*.xml</param-value></context-param>
Programming:
Spring provides three implementations for applicationcontext:
Classpathxmlapplicationcontext filesystemxmlapplicationcontextXmlwebapplicationcontext (customized for Web applications)
1,Classpathxmlapplicationcontext
// Eg1. load a single configuration applicationcontext CTX = new classpathxmlapplicationcontext ("bean. XML "); // eg2. load multiple configurations string [] Locations = {" bean1.xml "," bean2.xml "," bean3.xml "}; applicationcontext ctx2 = new classpathxmlapplicationcontext (locations );
2,Filesystemxmlapplicationcontext
// Eg1. load a single configuration file (loaded from the current working directory through the file system) applicationcontext CTX = new filesystemxmlapplicationcontext ("bean. XML "); // eg2. load multiple configuration files (loaded from the file system relative to the current working directory) string [] Locations = {" bean1.xml "," bean2.xml ", "bean3.xml"}; applicationcontext ctx2 = new filesystemxmlapplicationcontext (locations); // eg3. load a single configuration file (based on a specific path) applicationcontext ctx3 = new filesystemxmlapplicationcontext ("D: /project/bean. XML ");
3,Xmlwebapplicationcontext
ServletContext servletContext = request.getSession().getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Summary:
The first two classes are applicable to independent applications that use the Spring framework and require the program to manually initialize spring through the configuration file. 3. It is suitable for B/S systems that adopt the Spring framework. The applicationcontext object is obtained through the servletcontext object, and then the desired class instance is obtained through it.
1 and 2 throw an exception when the query fails. The third method returns NULL.
New classpathxmlapplicationcontext ("applicationcontext. xml"); allocates memory to all factory management beans. In Web applications, if the above code is not a singleton or the class that uses this code is not
For a single instance, the memory is allocated to the configured bean. If the sessionfactory of Hibernate is also managed by spring, the entity will consume more memory. The solution is to use the singleton mode.