简体   繁体   English

Spring MVC servlet上下文中的变量?

[英]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. 对于Spring MVC项目,我希望减少在servlet上下文中切换服务器,路径等时的错误和时间。

Is there a way to store variables in the servlet context (ie servlet-context.xml )? 有没有一种方法可以将变量存储在servlet上下文中(即servlet-context.xml )?

Example

VARIABLE is used to to switch the server url, user, and password in myDataSource VARIABLE用于在myDataSource中切换服务器的url,用户和密码

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>

Maybe I'm misunderstanding your question, but my answer to 也许我误会了您的问题,但是我对

Is there a way to store variables in the servlet context (ie servlet-context.xml)? 有没有一种方法可以将变量存储在servlet上下文中(即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. 看到这里这里。

The way of implementing it in XML is modifying the web.xml and servlet-context.xml . 用XML实现它的方法是修改web.xmlservlet-context.xml

Solution: 解:

In web.xml add a new context-param for spring.profiles.active . web.xmlspring.profiles.active添加一个新的context-param 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. servlet-context.xml您将使用概要文件包装bean。 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. 此时,在概要文件bean引起错误之后定义的bean。 Thus, I had to move the java beans to a new file and imported them before the profile definitions. 因此,我不得不将java bean移到一个新文件,并在概要文件定义之前将其导入。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM