小编典典

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

tomcat

我正在设置一个Web服务,该服务将在同一雄猫中的多个不同应用程序中重复使用。我想要做的是建立一个设置,在其中可以重用servlet上下文XML,但只是让它根据正在设置的servlet选取正确的属性文件。我创建了PropertyPlaceholderConfigurer的子类,该子类将允许我与XML分开请求属性文件,但是我无法弄清楚如何从此类中获取servlet名称。

使用Spring MVC 3.2.8甚至可以做到这一点还是有更好的方法吗?

谢谢


阅读 347

收藏
2020-06-16

共1个答案

小编典典

我最终设法弄清楚了。相反,我要做的是使用一个包含大多数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加载的任何属性。

2020-06-16