在Spring,Maven和Eclipses中处理XML文件中的属性值的最佳方法 [英] Best ways to deal with properties values in XML file in Spring, Maven and Eclipses

查看:133
本文介绍了在Spring,Maven和Eclipses中处理XML文件中的属性值的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Spring WebFlow项目,它在XML文件中具有很多属性值,就像Spring程序员所知道的那样。我有数据库用户名,密码,URL等。



我们正在使用Eclipse与Spring WebFlow和Maven。我们试图让SA做这些构建,但SA不想进入XML文件来更改值,但另一方面,我们不知道生产值。我们如何使用这个?

解决方案

大多数SA更愿意和自信地处理 .properties 文件而不是 .xml



Spring提供 PropertyPlaceholderConfigurer ,让您将所有内容定义为一个或多个 .properties 文件,并将占位符替换为 applicationContext.xml



src / main / resources / 文件夹下创建一个 app.properties

  ... 

#Dadabase连接设置:
jdbc.driverClassName = org.postgresql。驱动
jdbc.url = jdbc:postgresql:// localhost:5432 / app_db
jdbc.username = app_admin
jdbc.password = password

...。 ..

并在<$ c中使用PropertyPlaceholderConfigurer $ c> applicationContext.xml 像这样:

  ... ... 

< bean class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer>
< property name =location>
< value> app.properties< / value>
< / property>
< / bean>

... ...

< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =urlvalue =$ {jdbc.url}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< / bean>

查看 Spring PropertyPlaceholderConfigurer示例详细信息。



此外,从应用程序部署的角度来看,我们通常将应用程序打包在一些可执行格式和.properties文件通常打包在可执行的war或ear文件中。一个简单的解决方案是配置您的PropertyPlaceholderConfigurer bean以预定义的顺序从多个位置解析属性,因此在部署环境中,可以使用固定位置或环境变量来指定属性文件,还要注意为了简化对于SA的部署/配置任务,我们通常使用一个外部的.properties文件来定义所有运行时配置,如下所示:

  ; bean class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer> 
< property name =locations>
< list>
<! - 战争文件中的默认位置 - >
< value> classpath:app.properties< / value>
<! - 环境特定的位置,服务器上的固定路径>
< value> file:///opt/my-app/conf/app.properties< / value>
< / list>
< / property>
< property name =ignoreResourceNotFoundvalue =true/>
< / bean>

希望这有帮助。


I am working on a Spring WebFlow project which has a lot of property values in XML files, as any Spring programmer knows. I have database user names, password, URLs, etc.

We are using Eclipse with Spring WebFlow and Maven. We are trying to have an SA do the builds but the SA does not want to go into the XML files to change the values, but on the other hand, we don't know the production values. How do we work with this?

解决方案

Most SA are more willing and confident to deal with .properties file rather than .xml.

Spring provide PropertyPlaceholderConfigurer to let you define everything into one or several .properties file and substitute the placeholder in applicationContext.xml.

Create a app.properties under src/main/resources/ folder:

... ...

# Dadabase connection settings:
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/app_db
jdbc.username=app_admin
jdbc.password=password

... ...

And use PropertyPlaceholderConfigurer in applicationContext.xml like so:

... ...

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
    <value>app.properties</value>
  </property>
</bean>

... ...

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
</bean>

Check out Spring PropertyPlaceholderConfigurer Example for more details.

In addition, from application deployment perspective, we usually package app in some executable format and the .properties files are usually packed inside the executable war or ear file. A simple solution is to configure your PropertyPlaceholderConfigurer bean to resolve properties from multiple location in a pre-defined order, so in the deployment environment, you can use a fixed location or environment variable to specify the properties file, also note that in order to simplify the deploy/configure task for SA, we usually use a single external .properties file define all runtime configuration, like so:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
      <!-- Default location inside war file -->
      <value>classpath:app.properties</value>
      <!-- Environment specific location, a fixed path on server -->
      <value>file:///opt/my-app/conf/app.properties</value>
    </list>
  </property>
  <property name="ignoreResourceNotFound" value="true"/>
</bean>

Hope this helps.

这篇关于在Spring,Maven和Eclipses中处理XML文件中的属性值的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆