java Web 上下文根是否有内置的 Spring 环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6931585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:56:22  来源:igfitidea点击:

Is there a built-in Spring environment variable for the web context root?

javaspringspring-wscontextpath

提问by Greg

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?

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

Something like:

就像是:

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

回答by beny23

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

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

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

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

如果您使用的是 pre-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>

and inside your myWebservices bean

在你的 myWebservices bean 中

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

EDIT:

编辑:

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

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

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

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

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

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

回答by Prasanna Talakanti

I had a similar problem that you described, I use property files to do this

我遇到了您描述的类似问题,我使用属性文件来执行此操作

  • ws_dev.properties
  • ws_prod.properties
  • ws_dev.properties
  • ws_prod.properties

I configured my property file like this, The deployment property is java vm argument like

我像这样配置了我的属性文件,部署属性是 java vm 参数,如

-Ddeployment=dev

<context:property-placeholder location="ws_${deployment}.properties"/>

回答by Matthias Hübner

May be late, but may some other need a solution too:

可能会迟到,但其他一些人也可能需要解决方案:

set property in servlet:

在 servlet 中设置属性:

web.xml

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

The bean declaration in spring-ws-context.xml:

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>

回答by Eugene Kuleshov

You can add ApplicationContextAware interface to your bean, cast it to WebApplicationContext and then get ServletContext. Also see class org.springframework.web.context.ContextLoader

您可以将 ApplicationContextAware 接口添加到您的 bean,将其转换为 WebApplicationContext,然后获取 ServletContext。另见类 org.springframework.web.context.ContextLoader