Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring-PropertyPlaceHolderConfigure-根据环境选择属性文件_Spring_Spring Mvc - Fatal编程技术网

Spring-PropertyPlaceHolderConfigure-根据环境选择属性文件

Spring-PropertyPlaceHolderConfigure-根据环境选择属性文件,spring,spring-mvc,Spring,Spring Mvc,我需要根据环境(dev、qa或prod)选择属性上下文文件,下面是我对PropertyPlaceholderConfigurer的bean配置 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>

我需要根据环境(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>

任何帮助都将不胜感激

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

  • 查看springs属性注入。您可以在预定义的位置定义属性,只需确保正确的属性存在于正确的框中即可
  • 如果不想这样做,可以考虑将环境类型作为JVM参数注入(例如,-Denv.type=PROD)或类似的东西。然后可以在spring中使用此属性。看看如何做到这一点

  • 最后,我可以基于使用maven概要文件的环境打包所需的.properties。我对开发人员、qa和产品使用了不同的配置文件,如下所示

    <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>
    
    
    发展
    真的
    maven资源插件
    2.4
    复制开发人员资源
    过程资源
    复制资源
    真的
    ${project.basedir}/WebContent/WEB-INF/config
    ${project.basedir}/WebContent/WEB-INF/config/dev
    
    当前我通过JVM参数注入属性的可能重复项。但是当我在apache tomcat或WAS 7服务器中部署时,我不确定如何传递这个JVM参数。但是现在,自从我将其迁移到maven之后,我正在寻找在maven中也可以这样做的方法。在tomcat中,您可以通过在catalina.bat或catalina.sh中将其设置为来添加系统属性,只需将其添加到SET JAVA_OPTS=-Denv=QA即可
    <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>