简体   繁体   English

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

[英]Setting up multiple web services using the same servlet context

I am setting up a web service which will be reused for several different apps within the same tomcat. 我正在设置一个Web服务,该服务将在同一雄猫中的多个不同应用程序中重复使用。 What I am looking to do is have a setup where I can reuse the servlet context XML but just have it pick up the correct properties file based on the servlet being setup. 我想要做的是建立一个设置,在其中可以重用servlet上下文XML,但只是让它根据正在设置的servlet选取正确的属性文件。 I have created a subclass of PropertyPlaceholderConfigurer which will allow me to request the properties file separately from the XML but I cannot figure out how to get the servlet name from within this class. 我创建了PropertyPlaceholderConfigurer的子类,该子类将允许我与XML分开请求属性文件,但是我无法弄清楚如何从此类中获取servlet名称。

Is this even possible or is there a better way to do this using Spring MVC 3.2.8? 使用Spring MVC 3.2.8甚至可以做到这一点还是有更好的方法吗?

Thanks 谢谢

I managed to figure this out eventually. 我最终设法弄清楚了。 What I did instead was to use a single XML file which contained most of the servlet configuration and then had individual XML files which just set the properties file to use. 相反,我要做的是使用一个包含大多数Servlet配置的单个XML文件,然后使用一个单独的XML文件来设置要使用的属性文件。 Then in the web.xml I specified both of the XML files in the param-value for the servlet and they both get loaded and it uses the correct properties file. 然后在web.xml中,我在servlet的param-value中指定了两个XML文件,它们都被加载,并且使用正确的属性文件。 Eg web.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>

Where properties-context.xml 其中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>

And servlet-context.xml is a regular Spring servlet context file which uses ${} for any properties to load from my.properties. servlet-context.xml是常规的Spring servlet上下文文件,它使用$ {}来从my.properties加载的任何属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM