Setting up multiple web services using the same servlet context

谁说胖子不能爱 提交于 2019-12-25 07:11:11

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/23355560/setting-up-multiple-web-services-using-the-same-servlet-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!