如何为具有不同映射的一个servlet配置不同的Web应用程序上下文

时间:2014-11-27 14:24:12

标签: java spring servlets applicationcontext

首先,我有一个通用的http servlet,我想在特定的上下文中运行,具体取决于URL映射(所以我切换到了HttpRequestHandler实现)。请考虑简化示例。在web.xml中,我有以下配置:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>testServlet1</servlet-name>
    <servlet-class>local.TestServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>testServlet2</servlet-name>
    <servlet-class>local.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>testServlet1</servlet-name>
    <url-pattern>/test1/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>testServlet2</servlet-name>
    <url-pattern>/test2/*</url-pattern>
</servlet-mapping>

我的servlet是HttpRequestHandler的实现,看起来像这样:

public class TestRequestHandler implements HttpRequestHandler {

private Counter counter;

public void setCounter(Counter counter) {
    this.counter = counter;
}

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    writer.write("<h1>Hello World</h1>");
    writer.write("<h1>" + counter.getName() + ": " + counter.getValue() + "</h1>");
}
}

所以我想用不同的Counters bean初始化servlet bean(testServlet1和testServlet2),并为普通bean和两个可以扩展和覆盖的单独的 servlet上下文创建一个根上下文(applicationContext.xml)根上下文(testServlet1-servlet.xml和testServlet2-servlet.xml)。所以配置应该是这样的:

<bean id="counter" class="local.Counter">
    <property name="name" value="CounterA"/>
</bean>

<bean id="testServlet1" class="local.TestRequestHandler">
    <property name="counter" ref="counter"/>
</bean>

<bean id="counter" class="local.Counter">
    <property name="name" value="CounterB"/>
</bean>

<bean id="testServlet2" class="local.TestRequestHandler">
    <property name="counter" ref="counter"/>
</bean>

是否有可能以及如何实施此类配置?我想过DispatcherServlet,但不了解如何配置HttpRequestHandler的实现。

1 个答案:

答案 0 :(得分:1)

我找到了解决我任务的方法。简而言之 - 我们创建了两个DispatcherServlet,每个servlet配置都有自己的上下文,它扩展了根上下文。这是配置:

web.xml - 设置两个DispatcherServlet

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>dispatcher2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher1</servlet-name>
    <url-pattern>/test1</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dispatcher2</servlet-name>
    <url-pattern>/test2</url-pattern>
</servlet-mapping>

applicationContext.xml - 根上下文和公共bean在这里

<bean id="counter" class="local.Counter">
    <property name="name" value="CounterC"/>
</bean>

dispatcher1-servlet.xml - 覆盖自己的上下文中常见bean和特定配置的实现

<bean id="counter" class="local.Counter">
    <property name="name" value="CounterA"/>
</bean>

<bean name="testServlet" class="local.TestRequestHandler">
    <property name="counter" ref="counter"/>
</bean>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/test1">testServlet</prop>
        </props>
    </property>
</bean>