我正在使用Spring Web Services将服务公开为Web服务。在我的spring配置xml文件中,我有一个bean,它是DefaultWsdl11Definition的一个实例。需要设置的其中一个属性是 locationUri 。这需要是一个完全合格的Uri,但是当应用程序从dev升级到uat和生产时,我不想更改此值。 Spring知道Web应用程序上下文根是什么,所以我可以在配置文件中指定一个变量来访问它吗?
类似的东西:
<bean id="myWebServices"
class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
<property name="schemaCollection">
<bean
class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
<property name="xsds" ref="xsdList"/>
<property name="inline" value="true" />
</bean>
</property>
<property name="portTypeName" value="myWebServices" />
<property name="locationUri" value="${webContextRoot}/webServices" />
</bean>
答案 0 :(得分:9)
使用Spring 3.0,您应该能够通过Web应用程序上下文中的servletContext bean访问servlet上下文:
<property name="locationUri" value="#{servletContext.contextPath}/webServices" />
如果您使用的是Spring-EL之前(3.0之前),您应该可以
<bean name="servletContextBean" class="org.springframework.web.context.support.ServletContextFactoryBean" />
<bean name="contextPath" factory-bean="servletContextBean" factory-method="getContextPath" />
<bean name="locationUri" factory-bean="contextPath" factory-method="concat">
<constructor-arg value="/webServices" />
</bean>
并在myWebservices bean中
<property name="locationUri" ref="locationUri" />
编辑:
我不认为从ServletContext获取服务器名称和端口,因为根据设置,Web容器可能不知道主机名(即HTTP服务器可能在Web容器前面,例如tomcat可能落后Apache Web服务器或取决于Websphere配置。
但是,以下内容可能是获取主机名的解决方案的一部分。使用Spring 3.0,您可以执行以下操作:
<property name="host"
class="java.net.InetAddress"
factory-method="getLocalHost"/>
<property name="locationUri"
value="http://#{host.canonicalHostName}:8080/#{servletContext.contextPath}/webServices" />
答案 1 :(得分:3)
我遇到了类似的问题,我使用属性文件来执行此操作
我配置了我的属性文件,部署属性是java vm参数,如
-Ddeployment=dev
<context:property-placeholder location="ws_${deployment}.properties"/>
答案 2 :(得分:2)
可能会迟到,但其他人也可能需要解决方案:
在servlet中设置属性:
的web.xml
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-ws-context.xml</param-value>
</init-param>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
spring-ws-context.xml中的bean声明:
<bean id="WebService"
class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition"
p:portTypeName="App" p:locationUri="/WebServices" p:requestSuffix="Request"
p:responseSuffix="Response">
<property name="schema">
<bean class="org.springframework.xml.xsd.SimpleXsdSchema" p:xsd="classpath:/requestTypes.xsd" />
</property>
</bean>
答案 3 :(得分:-1)
您可以将ApplicationContextAware接口添加到您的bean,将其转换为WebApplicationContext,然后获取ServletContext。另请参阅org.springframework.web.context.ContextLoader类