首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring MVC中设置环境/配置文件

问在Spring MVC中设置环境/配置文件
EN

Stack Overflow用户
提问于 2017-01-30 03:35:43
回答 1查看 2K关注 0票数 0

我有一个Spring4MVC应用程序,我想在其中从命令行传入环境(配置文件),并让它在启动时读取正确的特定于环境的.properties文件。

这些属性文件本质上包含不同的jdbc连接字符串,因此每个环境都可以连接到正确的数据库。

总的来说,我还在学习Spring和Java,所以很难弄清楚这一点。

在web.xml中,我定义了3个环境/配置文件

代码语言:javascript
复制
<context-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>test, dev, prod</param-value>
</context-param>

我在resources目录下有3个特定于环境的文件。我的理解是,Spring将尝试获取适当的特定于环境的文件和一个通用的application.properties文件(如果有,这里没有)作为后备。

代码语言:javascript
复制
> \ls src/main/webapp/resources/properties/
application-dev.properties  application-prod.properties application-test.properties

每个文件都非常简单,只是针对该环境的jdbc连接参数。示例:

代码语言:javascript
复制
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/galapagos
jdbc.username=foo
jdbc.password=

最后,在servlet文件spring-web-servlet.xml中,我读取了应用程序属性文件,并使用它来建立连接

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

  ....

  <!-- Database / JDBC -->

  <beans:bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <beans:property name="location" value="resources/properties/application.properties" />
  </beans:bean>

  <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <beans:property name="driverClassName" value="${jdbc.driverClassName}" />
    <beans:property name="url" value="${jdbc.url}" />
    <beans:property name="username" value="${jdbc.username}" />
    <beans:property name="password" value="${jdbc.password}" />
  </beans:bean>

  ....

</beans:beans>

这是错误的,因为它试图寻找一个不存在的resources/properties/application.properties。但我不确定还能放什么进去。如何让它在启动时动态读取正确的环境文件?

我看到了一些使用上下文监听器的like this one示例,但老实说,我还在学习Spring MVC,并不真正理解它们想要做什么

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-01-30 17:57:40

默认情况下,spring会尝试查找resources/properties/application.properties来加载属性。此文件是自动检测的。这是你的问题,你必须提供一个。如果您不喜欢application.properties文件,可以通过使用spring.config.location环境属性指定spring.config.name环境属性及其位置来覆盖它的名称。

在你的web.xml中,

代码语言:javascript
复制
<context-param>
 <param-name>spring.profiles.active</param-name>
 <param-value>test, dev, prod</param-value>
</context-param>

您正在同时激活3个配置文件。我建议定义在您的application.properties中激活哪个配置文件。例如:

代码语言:javascript
复制
spring.profiles.active=dev

然后,特定的环境文件将被加载,并且将优先于默认的属性文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41925090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档