Introduction to spring applicationcontext

Source: Internet
Author: User

 

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 isApplicationContextInterface. It consistsBeanFactoryAnd providesBeanFactoryAll functions. In order to work in a more framework-oriented way and to layer and implement context inheritance,ApplicationContextProvides 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. InheritedResourceLoaderThe sub-interface resourcepatternresolver of the API.
  • Event support. InheritedApplicationEventPublisherInterface.
  • International support. InheritedMessageSourceInterface.

    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.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.