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

Java 在Spring MVC中设置环境/配置文件,java,spring,spring-mvc,servlets,Java,Spring,Spring Mvc,Servlets,我有一个Spring4MVC应用程序,我想在其中从命令行传入环境(概要文件),并让它在启动时读取正确的特定于环境的.properties文件 这些属性文件本质上包含不同的jdbc连接字符串,因此每个环境都可以连接到正确的数据库 我仍然在学习Spring和Java,所以很难弄清楚这一点 在web.xml中,我定义了3个环境/配置文件 <context-param> <param-name>spring.profiles.active</param-name>

我有一个Spring4MVC应用程序,我想在其中从命令行传入环境(概要文件),并让它在启动时读取正确的特定于环境的
.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>
每个文件都非常简单,只是用于该环境的jdbc连接参数。例如:

jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/galapagos
jdbc.username=foo
jdbc.password=
最后,在我的servlet文件
springwebservlet.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
。但我不知道还要放什么进去。如何让它在启动时动态读取正确的环境文件

我看到了一些使用上下文侦听器的示例,但老实说,我仍在学习SpringMVC,并不真正理解这些示例的作用


谢谢

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

web.xml

<context-param>
 <param-name>spring.profiles.active</param-name>
 <param-value>test, dev, prod</param-value>
</context-param>
然后,将加载特定的环境文件,并优先于默认属性文件。

Spring不会查找application.properties默认情况下,由Spring引导,请不要误导其他人
<context-param>
 <param-name>spring.profiles.active</param-name>
 <param-value>test, dev, prod</param-value>
</context-param>
spring.profiles.active=dev