Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

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>
like image 425
Greg Avatar asked Aug 03 '11 18:08

Greg


People also ask

What is the default root context path in Spring boot?

Overview. Spring Boot, by default, serves content on the root context path (“/”). While it's usually a good idea to prefer convention over configuration, there are cases when we do want to have a custom path. In this quick tutorial, we'll cover the different ways of configuring it.

How do you specify context root in Spring boot application?

By default, Spring boot applications are accessed by context path “/” which is default for embedded servers i.e. we can access the application directly at http://localhost:PORT/ . But in production, we will deploy the application under some context root – so that we can refer the URLs for other places.

What is root context in Spring?

The root-context in a Spring application is the ApplicationContext that is loaded by the ContextLoaderListener . This context should have globally available resources like services, repositories, infrastructure beans ( DataSource , EntityManagerFactory s etc.) etc.

How do I set context root in web xml?

To Set the Context RootA context root must start with a forward slash (/) and end with a string. In a packaged web module for deployment on the GlassFish Server, the context root is stored in glassfish-web. xml.


1 Answers

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

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>

and inside your 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).

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" />
like image 126
beny23 Avatar answered Sep 20 '22 07:09

beny23