我正在设置一个 Web 服务,该服务将在同一个 tomcat 中被多个不同的应用程序重复使用。我想要做的是有一个设置,我可以在其中重用 servlet 上下文 XML,但只是让它根据正在设置的 servlet 选择正确的属性文件。我创建了 PropertyPlaceholderConfigurer 的子类,它允许我从 XML 中单独请求属性文件,但我不知道如何从此类中获取 servlet 名称。
使用 Spring MVC 3.2.8 这甚至可能还是有更好的方法来做到这一点?
谢谢
最佳答案
我最终设法解决了这个问题。我所做的是使用一个包含大部分 servlet 配置的单个 XML 文件,然后使用单独的 XML 文件来设置要使用的属性文件。然后在 web.xml 中,我在 servlet 的参数值中指定了两个 XML 文件,它们都被加载并使用正确的属性文件。例如。网页.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 加载任何属性。
关于java - 使用相同的 servlet 上下文设置多个 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23355560/