部署针对不同环境使用Maven和春天(deployment for different enviro

2019-07-30 20:33发布

我有两个属性文件:

environment.properties:
 - project.host.db3.database.name=oracle

application.properties:
 - database.name=${project.host.db3.database.name}

第一个代表环境变量,第二个属性,以在弹簧项目中使用,在这个配置我尝试设置environment.properties但当然这是行不通的:

<bean id="systemPropertiesLoader"   
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" value="#{@systemProperties}" />
        <property name="targetMethod" value="putAll" />
        <property name="arguments">
    <util:properties location="classpath:environment.properties" />
</property>
</bean>

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="systemPropertiesLoader">
<property name="locations">
    <list>
        <value>classpath:application.properties</value>
    </list>
</property>

<!-- bean using database.name -->

是否可行?如果不是,人们如何在他们的项目(如database.name)无关的特性,并且只有一个文件(WAR,JAR等)进行部署?

Answer 1:

好了,现在看来,这是可行的,只要你定义你的属性是这样定义的XML豆:

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="systemPropertiesLoader">

但是,如果你曾经尝试从一个servlet访问属性:

this.getClass().getClassLoader().getResourceAsStream("application.properties");

没准你会得到这样的:

bad port configuration: ${project.host.db3.database.port}
java.lang.NumberFormatException: For input string: "${project.host.db3.database.port}"

在回答yorkw,现在我可以有相同的战争在多个环境中部署和配置与-Denvironment =开发主机,这样我就可以部署一个属性文件的开发,生产等,并简单地使用:

<bean id="systemPropertiesLoader"   
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" value="#{@systemProperties}" />
        <property name="targetMethod" value="putAll" />
        <property name="arguments">
    <util:properties location="classpath:**${environment}/**environment.properties" />
</property>
</bean>

否则,我应该部署在任何环境之前取代application.properties。 我敢肯定有比这更好的解决方案。



文章来源: deployment for different environments with maven and spring