简体   繁体   中英

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. 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. 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.

Is this even possible or is there a better way to do this using 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. 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. Eg 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

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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