Search code examples
xmlspringspring-mvcenvironment-variablesservletconfig

Variables in Spring MVC servlet context?


For Spring MVC project, I want to reduce errors and the amount of time when switching servers, paths, and etc in the servlet context.

Is there a way to store variables in the servlet context (i.e. servlet-context.xml)?

Example

VARIABLE is used to to switch the server url, user, and password in myDataSource

VARIABLE = "GOOGLE" // Server type: GOOGLE, YAHOO, BING. This will switch the server url, user, and password in myDataSource

<beans:bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <beans:property name="driverClassName" value="${jdbc.sqlserver.driver}" />
    <beans:property name="url" value="${jdbc.**VARIABLE**.url}" />
    <beans:property name="username" value="${jdbc.**VARIABLE**.user}" />
    <beans:property name="password" value="${jdbc.**VARIABLE**.pw}" />
</beans:bean>

Solution

  • Maybe I'm misunderstanding your question, but my answer to

    Is there a way to store variables in the servlet context (i.e. servlet-context.xml)?

    is "No". These context configuration files are meant to be static.

    What you should do instead is use Profiles. See here and here.