Web上下文根是否有内置的Spring环境变量? [英] Is there a built-in Spring environment variable for the web context root?

查看:157
本文介绍了Web上下文根是否有内置的Spring环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Web Services将服务公开为Web服务。在我的spring配置xml文件中,我有一个bean,它是DefaultWsdl11Definition的一个实例。需要设置的其中一个属性是 locationUri 。这需要是一个完全合格的Uri,但是当应用程序从dev升级到uat和生产时,我不想更改此值。 Spring知道Web应用程序上下文根是什么,所以我可以在配置文件中指定一个变量来访问它吗?

I'm using Spring Web Services to expose a service as a web service. In my spring configuration xml file, I have a bean which is an instance of DefaultWsdl11Definition. One of the properties that needs to be set is the locationUri. This needs to be a fully qualified Uri, but I don't want to have to change this value when the application gets promoted from dev to uat and to production. Spring knows what the web application context root is, so is there a variable that I can specify in my configuration file to access it?

类似于:

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


推荐答案

使用Spring 3.0,你应该能够访问servlet上下文通过Web应用程序上下文中的servletContext bean:

With Spring 3.0, you should be able to access the servlet context through the servletContext bean in the web application context:

<property name="locationUri" value="#{servletContext.contextPath}/webServices" />

如果您使用的是Spring-EL之前(3.0之前),您应该能够做到

If you're using pre-Spring-EL (before 3.0), you should be able to do

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

and inside your myWebservices bean

<property name="locationUri" ref="locationUri" />

编辑:

我不知道想从ServletContext获取服务器名称和端口,因为根据设置,Web容器可能不知道主机名(即HTTP服务器可能在Web容器前面,例如tomcat可能在Apache Web服务器后面或取决于Websphere配置。)

I don't think getting the server name and port from the ServletContext, as depending on the setup the web container may not know the hostname (i.e. a HTTP server may be in front of the web container, e.g. tomcat may be behind an Apache web server or depending on the Websphere configuration).

但是,以下内容可能是获取主机名的解决方案的一部分。使用Spring 3.0,您可以执行以下操作:

However, the following may be part of a solution to get the hostname. With Spring 3.0, you could do the following:

<property name="host" 
          class="java.net.InetAddress" 
          factory-method="getLocalHost"/>

<property name="locationUri" 
          value="http://#{host.canonicalHostName}:8080/#{servletContext.contextPath}/webServices" />

这篇关于Web上下文根是否有内置的Spring环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆