若是您想要在本身所定義的Servlet類別中使用Spring的容器功能,則也可使用 org.springframework.web.context.ContextLoaderListener,例如在web.xml中使用< listener>標籤加以定義:
...
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
...
ContextLoaderListener預設會讀取applicationContext.xml,您能夠指定本身的定義檔,只要在<context-param>中指定"contextConfigLocation"參數,例如:
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-config.xml,
→ /WEB-INF/demo-service.xml</param-value>
</context-param>
...
接着您能夠在自定義的Servlet中使用 org.springframework.web.context.support.WebApplicationContextUtils,從 ServletContext中取得org.springframework.web.context.WebApplicationContext,例如:
WebApplicationContext ctx =
WebApplicationContextUtils.
getRequiredWebApplicationContext(
this.getServletContext());
WebApplicationContext實做了ApplicationContext介面,是Spring專爲Servlet的Web應用程式設計的 ApplicationContext實做類別,在取得WebApplicationContext以後,您能夠利用它來取得Bean定義檔中定義的 Bean實例,例如:
Date date = (Date) ctx.getBean("dateBean");
在不支援Listener設定的容器上(例如Servlet 2.2以更早的版本),您可使用org.springframework.web.context.ContextLoaderServlet來取代上面的ContextLoaderListener的設定,例如:
...
<servlet>
<servlet-name>contextLoader</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
綜合以上的敘述,撰寫一個簡單的範例來示範完整的組態方式,假設您撰寫了一個簡單的Servlet程式,做用爲提供簡單的報時功能,以下所示:
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"));
}
}
這個Servlet中使用了WebApplicationContext來嘗試取得dateBean,您能夠在web.xml中定義ContextLoaderListener與Bean定義檔的位置,例如:
<?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>
在<context-param>的"contextConfigLocation"屬性中,設定了Bean定義檔的位置與名稱,Bean定義檔的內容以下所示:
<?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>