骡子3 - 从.properties文件加载

时间:2013-06-25 17:52:39

标签: mule mule-studio

我正在使用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文件?

5 个答案:

答案 0 :(得分:7)

将xmlns名称空间前缀和架构位置添加到Mule config mule元素标记。

前缀:

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

SchemaLocation:

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>

答案 1 :(得分:4)

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

  1. 保存src / main / resources
  2. 下的所有.properties
  3. &LT; context:property-placeholder location =“file.dev.properties,file.stage.properties”/&gt;
  4. 将所有文件保存在类路径中是一项挑战。所以转到你的项目文件夹,在文本板中打开.classpath文件并添加以下行

    < classpathentry 
      including="file.dev.properties|file.prod.properties|file.stage.properties"
      kind="src" path="src/main/resources"/ >
    
  5. 保存,刷新并且有效。

答案 2 :(得分:0)

答案 3 :(得分:0)

其他答案涵盖了名称空间问题,但我要补充一点,我发现上下文: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>

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

答案 4 :(得分:0)

只需将属性文件放在资源文件夹和

在property-placeholder中使用此“classpath:settings.properties”,它将起作用...