【问题标题】:Trying to setup externalizing properties in spring尝试在春季设置外部化属性
【发布时间】:2010-03-26 14:57:03
【问题描述】:

我正在用 maven 构建我的项目,所以根据 maven 方式,配置应该在 src/main/conf 中,我怎么能对我的 spring 应用程序上下文说那是找到 jdbc.properties 的地方?这是示例 bean:

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

Spring 假设此配置位于 src/main/webapp/WEB-INF 中,希望我已经清楚,如果不是,我会重新表述我的问题,谢谢

【问题讨论】:

    标签: java spring


    【解决方案1】:

    我正在使用 maven 构建我的项目,所以 根据maven方式,配置应该 在 src/main/conf 中

    实际上,配置数据通常应该放在 src/main/resources 中,这样它将在类路径中,您可以引用您的属性文件,例如:

    <property name="location" value="classpath:jdbc.properties" />
    

    【讨论】:

    • @Kevin 实际上配置应该在 src/main/config 和资源应该在 src/main/resources javaworld.com/javaworld/jw-12-2005/jw-1205-maven.html?page=1
    • @c0mrade 这样的配置文件可以说是“资源”
    • @matt b 是的,可以说,你说它应该进入资源我认为它应该进入配置,毕竟所有资源可能已经充满了一些东西
    • 这也不好用 .. 如果我什至将属性文件放在 src/main/resources 中 ..
    • Kevin 的回答正是我对多个项目的设置。我在 src/main/resources 中有一个 runtime.properties 文件和一个 propertyConfigurer bean。唯一的区别是我使用'locations'属性: 类路径:runtime.properties
    【解决方案2】:

    我认为“配置文件”的含义还不是很清楚。我认为这是其他 maven 插件(例如surefire插件、程序集插件等)使用的配置文件。

    当然,在我处理过 bean 文件的 10 多个 Web 应用项目中,jdbc.properties 文件都在 src/main/resources 下

    【讨论】:

      【解决方案3】:

      试试这个

      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location" value="src/main/config/jdbc.properties" />
      </bean>
      

      【讨论】:

      • 这仅在最终构建的项目将 jdbc.properties 置于src/main/config 下时才有效(这不太可能且不规则)
      • @c0mrade,那篇文章呢?你是正确的,规定的约定是“配置文件”进入 src/main/config.xml 文件。我指的是,当您打包应用程序时,配置文件打包在打包文件(jar/war/etc.)中名为src/main/config 的文件夹中。因此,您的答案仅在您在源代码中运行应用程序时,或者当src/main/config 在类路径中(默认情况下不是)时才有效。正确的前缀是使用classpath: 或其他位置。
      • 另外,如果你想向人们介绍约定是什么,最好参考maven.apache.org/guides/introduction/…,而不是 2005 年的 javaworld 文章。
      • @matt b 你说得对,但 maven guide 也说 src/main/config Configuration files
      猜你喜欢
      • 2015-01-08
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2010-11-20
      • 2021-04-20
      • 1970-01-01
      相关资源
      最近更新 更多