user2601995
user2601995

Reputation: 6813

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>

Upvotes: 0

Views: 2043

Answers (2)

user2601995
user2601995

Reputation: 6813

The way of implementing it in XML is modifying the web.xml and servlet-context.xml.

Solution:

In web.xml add a new context-param for spring.profiles.active. This will be used as the profile selector.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml
    </param-value>
</context-param>
<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>DEV-PROFILE</param-value><!-- profile name goes here -->
</context-param>


In the servlet-context.xml you'll wrap the beans with the profile. Here, I'm giving a Development and Test profile for each database connection.

<beans:beans profile="DEV-PROFILE">
    <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.dev.url}" />
        <beans:property name="username" value="${jdbc.dev.user}" />
        <beans:property name="password" value="${jdbc.dev.pw}" />
    </beans:bean>
</beans:beans>
<beans:beans profile="TEST-PROFILE">
    <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.test.url}" />
        <beans:property name="username" value="${jdbc.test.user}" />
        <beans:property name="password" value="${jdbc.test.pw}" />
    </beans:bean>
</beans:beans>


At this point beans defined after the profile beans caused errors. Thus, I had to move the java beans to a new file and imported them before the profile definitions.

<beans:import resource="servlet-beans.xml"/>

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280174

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.

Upvotes: 1

Related Questions