我有两个属性文件:
environment.properties: - project.host.db3.database.name=oracle application.properties: - database.name=${project.host.db3.database.name}第一个表示环境变量,第二个表示春季项目中使用的属性,在此配置中,我尝试设置environment.properties,当然,它不起作用:
The first one represents the environment variables and the second one the properties to be used in a spring project, in this configuration i try to set the environment.properties but of course it doesn't work:
<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 -->是否可行?如果没有,人们如何在项目中拥有不可知的属性(如数据库.name),只有一个文件(war,jar等)要部署?
Is it doable?, and if not, how do people have agnostic properties in their projects (like database.name), and only one file (war, jar, etc.) to be deployed?
推荐答案只要你定义你的属性就可以这样定义bean xml:
Well, it seems it's doable for beans xml defined as long as you define your properties it like this:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="systemPropertiesLoader">但是,如果您尝试从servlet访问属性:
But if you ever try to access the properties from a 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 =开发,所以我可以部署一个属性文件进行开发,生产等,只需使用:
In answer to yorkw, now i can have the same war to be deployed in several environments and configure the host with -Denvironment=development, so i can deploy a properties file for development, production, etc. and simply use:
<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替换为每个环境。我相信有更好的解决方案。
Otherwise i should have the application.properties substituted before deployment for every environment. I'm sure there are better solutions than this.
更多推荐
部署不同的环境与春天和春天
发布评论