SSH FAQ Series (i) How the--spring container is started _spring

Source: Internet
Author: User
Tags jboss

One of the reasons the SSH framework is very popular is that spring can be seamlessly integrated with the STRUTS2 framework. When you use spring, you do not need to create a Web container manually, but instead create a spring container through a profile declaration.

In Web applications, there are two ways to create a spring container: To configure the use of the third party MVC framework in Web.xml to create

The first is more commonly used, the second method is only valid for a specific framework, this is not an introduction, the following only describes the first method.

In order for the spring container to start automatically with Tomcat/jboss, we used the Servletcontextlistener listener to complete it.

Let's take a look at the web.xml of the project to integrate the spring configuration.

<!--integration Spring-->
  <listener>
        <listener-class> org.springframework.web.context.contextloaderlistener</listener-class>
    </listener>
    < context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> Classpath:spring/applicationcontext.xml</param-value>
    </context-param>

In the above configuration,
1, in Contextloaderlistener, the Contextloaderlistener listener implements the Servletcontextlistener interface. He automatically finds applicationcontext.xml files under Web/inf at the time of creation.

2, when need to import the configuration file has more than one need to make the Context-param label, to determine the profile file name. By looking for initialization parameters for contextconfiglocation.

public void contextinitialized (Servletcontextevent event) {
    //1, Load Spring's web container
        This.contextloader = Createcontextloader ();
    2, initialize the Spring Web container and load the configuration file.
        This.contextLoader.initWebApplicationContext (Event.getservletcontext ());
    }

Approximate process for creating a spring container:
    /**
     * Create Contextloader. Can is overridden in subclasses.
     * @return The new contextloader
     *
    /protected Contextloader Createcontextloader () {return
        new Contextloader ();
    }

Summary:
In the SSH framework, we can configure the spring container to start automatically with Tomcat/jboss, and so on, without having to manually open it. This is done through the listener, which automatically reads and loads the corresponding configuration file when the spring container is started.

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.