【问题标题】:Mule 3 - Loading from .properties fileMule 3 - 从 .properties 文件加载
【发布时间】:2013-06-25 17:52:39
【问题描述】:

我正在使用 Mule 3 使用 JDBC 查询数据库,我想根据 .properties 文件的输入来修改查询。我的 xml 中有这个...

<context:property-placeholder location="C:\path\to\file\settings.properties" />

得到以下异常...

Exception in thread "main" org.mule.module.launcher.DeploymentInitException: SAXParseException: The prefix "context" for element "context:property-placeholder" is not bound.

我需要包含一些特殊的 .xsd 文件吗?

【问题讨论】:

    标签: mule mule-studio


    【解决方案1】:

    将 xmlns 命名空间前缀和架构位置添加到您的 Mule 配置 mule 元素标签。

    前缀:

    xmlns:context="http://www.springframework.org/schema/context"
    

    架构位置:

    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd
    

    它应该如下所示。

    例如:

    <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:spring="http://www.springframework.org/schema/beans"      
        xmlns:http="http://www.mulesoft.org/schema/mule/http" 
        xmlns:context="http://www.springframework.org/schema/context"
    
        xsi:schemaLocation="
            http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.3/mule.xsd
            http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.3/mule-http.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd      
            ">
    
    
    <context:property-placeholder location="C:/path/to/file/settings.properties" />
    
    
      ...........  Other stuff
    
    
    
    </mule>
    

    【讨论】:

    • 谢谢。这似乎已经解决了命名空间问题,但是当文件明显存在时,我得到了 FileNotFound 异常。您以前遇到过这个问题吗?
    • 对这个问题提出了一个新问题,这里是链接stackoverflow.com/questions/17326783/…请看是否可以解决
    • 已为您的问题发布了答案。请尝试一下。那应该可以解决它。
    【解决方案2】:

    我遇到了同样的问题并修复了它。这就是我所做的。

    1. 将所有 .properties 保留在 src/main/resources 下
    2. 将所有文件保存在类路径中是一项挑战。所以转到您的项目文件夹,在文本键盘中打开 .classpath 文件并添加以下行

      < classpathentry 
        including="file.dev.properties|file.prod.properties|file.stage.properties"
        kind="src" path="src/main/resources"/ >
      
    3. 保存,刷新就可以了。

    【讨论】:

      【解决方案3】:

      【讨论】:

      • 我也有同样的异常。我应该补充一点,我将占位符放在 mule 标签本身内,即&lt;mule&gt;&lt;context:property-placeholder location="C:\path\to\file\settings.properties" /&gt;&lt;other stuff&gt;&lt;/mule 我应该将它放在对象内吗?
      【解决方案4】:

      其他答案涵盖了命名空间问题,但我要补充一点,我发现 context:property-placeholder 标记需要位于“spring:beans”标记之间。这是一个假设属性文件设置名为“jmsBrokerURL”的属性的示例:

      <mule xmlns="http://www.mulesoft.org/schema/mule/core" version="EE-3.4.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
            xmlns:spring="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="
                http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
                http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
          <spring:beans>
              <context:property-placeholder location="C:/path/to/file/settings.properties" />
              <spring:bean name="myConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
                  <spring:property name="brokerURL" value="${jmsBrokerURL}" />
              </spring:bean>
          </spring:beans>
      
          <flow name="MyFlow" doc:name="MyFlow">
              <!-- Flow configuration here. -->
          </flow>
      
      </mule>
      

      另一种读取属性的方法(也是我更喜欢的一种)是使用 Spring 的“util:properties”标签将属性读取到 Properties bean 中,然后使用 Spring EL 引用它。在这种情况下请注意,您使用 Spring EL“#{}”表示法而不是“${}”来引用对象及其变量。这是针对该技术修改的上述示例:

      <mule xmlns="http://www.mulesoft.org/schema/mule/core" version="EE-3.4.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
            xmlns:spring="http://www.springframework.org/schema/beans"
            xmlns:util="http://www.springframework.org/schema/util"
            xsi:schemaLocation="
                http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
      
          <spring:beans>
              <util:properties id="myConfig"  location="C:/path/to/file/settings.properties" />
              <spring:bean name="myConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
                  <spring:property name="brokerURL" value="#{myConfig.jmsBrokerURL}" /> <!-- Note the pound (hash) symbol. -->
              </spring:bean>
          </spring:beans>
      
          <flow name="MyFlow" doc:name="MyFlow">
              <!-- Flow configuration here. -->
          </flow>
      
      </mule>
      

      我喜欢后一种方法主要是因为我可以更轻松地处理多个属性文件并包含应用程序上下文文件。在处理多个属性文件或在另一个文件中包含应用程序上下文文件时,context:property-placeholder 标记可能会出现问题。

      【讨论】:

        【解决方案5】:

        只需将属性文件放在资源文件夹下即可

        在属性占位符中使用这个“classpath:settings.properties”,它将起作用...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-01
          • 1970-01-01
          • 2015-07-01
          • 2015-12-29
          • 1970-01-01
          • 2014-11-28
          相关资源
          最近更新 更多