【问题标题】:Spring - PropertyPlaceholderConfigurer - Select property file based on environmentSpring - PropertyPlaceholderConfigurer - 根据环境选择属性文件
【发布时间】:2012-06-06 05:47:33
【问题描述】:

我需要根据环境(dev、qa 或 prod)选择属性上下文文件,下面是我对PropertyPlaceholderConfigurer 的 bean 配置,

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>file:**/config/handsOn-${proj.env}.properties</value>
    </property>
</bean>

那么我怎样才能让 spring 框架根据它部署的环境来选择正确的文件。

我可以根据所部署的主机获取环境。使用下面的代码,

InetAddress.getLocalHost().getHostName()

任何帮助将不胜感激..!!

【问题讨论】:

标签: spring spring-mvc


【解决方案1】:

有几种方法可以做到这一点。

  1. 检查 springs 属性注入。您可以在预定义的位置定义属性,并确保正确的属性存在于右侧框
  2. 如果您不想这样做,请考虑将环境类型作为 JVM 参数注入(例如 -Denv.type=PROD)或类似的东西。然后你可以在春天使用这个属性。查看How do I read JVM arguments in the Spring applicationContext.xml 了解如何操作。

【讨论】:

  • 目前我正在通过 JVM 参数注入属性。但是当我在 apache tomcat 或 WAS 7 服务器中部署时,我不确定如何传递这个 JVM 参数。
  • 以前我使用 ant 脚本将内容从 handOn-dev.properties 移动到 handOn.properties 文件。但是现在自从我将它迁移到 Maven 之后,我正在寻找是否有任何方法可以在 Maven 中做同样的事情。
  • 在 tomcat 中,您可以通过在 catalina.bat 或 catalina.sh 中设置来添加系统属性,只需将其添加到 SET JAVA_OPTS=-Denv=QA
【解决方案2】:

最后,我可以使用 maven 配置文件根据环境打包所需的 .properties。我为 dev、qa 和 prod 使用了不同的配置文件,如下所示,

<profiles>
        <profile>
            <id>dev</id>
            <activation>
              <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>2.4</version>
                        <executions>
                            <execution>
                                <id>copy-dev-resources</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <!-- this is important -->
                                    <overwrite>true</overwrite>
                                    <!-- target -->
                                    <outputDirectory>${project.basedir}/WebContent/WEB-INF/config</outputDirectory>
                                    <resources>
                                        <resource>
                                            <!-- source -->
                                            <directory>${project.basedir}/WebContent/WEB-INF/config/dev</directory>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
</profiles>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 2013-08-21
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2018-05-29
    相关资源
    最近更新 更多