【问题标题】:Setting the environment/profile in Spring MVC在 Spring MVC 中设置环境/配置文件
【发布时间】:2017-01-29 19:35:43
【问题描述】:

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

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

总的来说,我仍在学习 Spring 和 Java,因此很难弄清楚这一点。

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

<context-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>test, dev, prod</param-value>
</context-param>

我在资源目录下有 3 个环境特定文件。我的理解是 Spring 将尝试获取适当的环境特定文件和一个通用的 application.properties 文件(如果存在,并且它不在这里)以供依赖。

> \ls src/main/webapp/resources/properties/
application-dev.properties  application-prod.properties application-test.properties

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

jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/galapagos
jdbc.username=foo
jdbc.password=

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

<?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,并不太了解那些试图做什么

谢谢!

【问题讨论】:

    标签: java spring spring-mvc servlets


    【解决方案1】:

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

    在你的web.xml

    <context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>test, dev, prod</param-value>
    </context-param>
    

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

    spring.profiles.active=dev
    

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

    【讨论】:

    • Spring默认不会查找application.properties,是Spring Boot会这样做,请不要误导他人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2017-05-05
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多