使用相同的servlet上下文设置多个Web服务

时间:2014-04-29 04:34:11

标签: java spring tomcat spring-mvc servlets

我正在设置一个Web服务,它将被重用于同一个tomcat中的几个不同的应用程序。我要做的是有一个设置,我可以重用servlet上下文XML,但只是让它根据正在设置的servlet选择正确的属性文件。我已经创建了一个PropertyPlaceholderConfigurer的子类,它允许我从XML中单独请求属性文件,但是我无法弄清楚如何从这个类中获取servlet名称。

这是否可行,或者使用Spring MVC 3.2.8有更好的方法吗?

由于

1 个答案:

答案 0 :(得分:0)

我最终设法解决了这个问题。我所做的是使用包含大部分servlet配置的单个XML文件,然后使用单独的XML文件来设置要使用的属性文件。然后在web.xml中,我在servlet的param-value中指定了两个XML文件,它们都被加载并使用正确的属性文件。例如。的web.xml

<servlet>
    <servlet-name>myAppServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/properties-context.xml /WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

其中properties-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:my.properties" />

</beans>

servlet-context.xml是一个常规的Spring servlet上下文文件,它使用$ {}来从my.properties加载任何属性。