其他答案涵盖了命名空间问题,但我要补充一点,我发现 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 标记可能会出现问题。