Webapplicationcontext: org. springframework. Web. Context. contextloaderlistener

Source: Internet
Author: User
You can also use Org. springframework. web. context. contextloaderlistener, for example, in the web. use the <listener> label to define in XML:

...
<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>
...


Contextloaderlistener reads applicationcontext. xml by default. You can specify your own definition file by specifying the "contextconfiglocation" parameter in <context-param>. For example:

...
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/beans-config.xml,
→/WEB-INF/demo-service.xml </param-value>
</Context-param>
...


Then you can use org. springframework. Web. Context. Support. webapplicationcontextutils in the Custom servlet to obtain org. springframework. Web. Context. webapplicationcontext from servletcontext, for example:

Webapplicationcontext CTX =
Webapplicationcontextutils.
Getrequiredwebapplicationcontext (
This. getservletcontext ());


Webapplicationcontext implements the applicationcontext interface. It is the applicationcontext implementation class designed by spring for servlet web applications. After obtaining the webapplicationcontext, you can use it to obtain the bean instance defined in the bean definition file. For example:
Date = (date) CTX. getbean ("datebean ");

On containers that do not support listener settings (for example, Servlet 2.2 is earlier), you can use org. springframework. Web. Context. contextloaderservlet to replace the preceding contextloaderlistener settings. For example:

...
<Servlet>
<Servlet-Name> contextloader </servlet-Name>
<Servlet-class>
Org. springframework. Web. Context. contextloaderservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
...


Based on the above description, write a simple example to demonstrate the complete configuration method. Assume that you have written a simple servlet program to provide a simple reporting function, as shown below:

  • Timeservlet. Java
package onlyfun.caterpillar;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.
support.WebApplicationContextUtils;

public class TimeServlet extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
WebApplicationContext ctx =
WebApplicationContextUtils.
getRequiredWebApplicationContext(
this.getServletContext());

PrintWriter out = res.getWriter();
out.println(ctx.getBean("dateBean"));
}
}


This servlet uses webapplicationcontext to try to obtain datebean. You can define the position of the contextloaderlistener and bean definition file in Web. XML, for example:

  • Web. xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
→ http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-config.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>time</servlet-name>
<servlet-class>
onlyfun.caterpillar.TimeServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>time</servlet-name>
<url-pattern>/time.do</url-pattern>
</servlet-mapping>
</web-app>


In the "contextconfiglocation" attribute of <context-param>, the location and name of the bean definition file are set. The content of the bean definition file is as follows:

  • Beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="dateBean" class="java.util.Date" singleton="false"/>
</beans>


You can connect to timeservlet. The result shows the connection time.

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.