部署不同的环境与春天和春天

编程入门 行业动态 更新时间:2024-10-07 20:32:57
本文介绍了部署不同的环境与春天和春天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个属性文件:

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.

更多推荐

部署不同的环境与春天和春天

本文发布于:2023-11-28 03:37:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:春天   环境

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!