Apache Camel - 从文件中读取 JDBC 数据源属性

Apache Camel - Read JDBC dataSource properties from file

我正在使用 Apache Camel 并尝试从此文件加载数据源属性

config.properties:

url = my_url
user = user_name
password = user_pass

这是数据源(blueprint.xml):

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
      <property name="URL" value="my_url"/>
      <property name="user" value="user_name"/>
      <property name="password" value="user_pass"/>
  </bean> 

如何从 config.properties 中读取值并将它们插入到数据源属性中?

根据代码,我假设您可能使用 spring 作为容器。 spring 中的一般解决方案是使用 PropertyPlaceHolder,您的配置将如下所示:

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

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
  <property name="URL" value="${jdbc.myUrl}"/>
  <property name="user" value="${jdbc.user_name}"/>
  <property name="password" value="${jdbc.user_pass}"/>
</bean> 

详情请查看example

你说的是 blueprint.xml 和 camel,所以我假设你在像 Karaf/ServiceMix 这样的 osgi 容器中,并且你正在使用 Aries Blueprint。

然后您可以使用 cm 命名空间和 property-placeholder。如果您使用 camel 并希望动态重新加载您的属性,那么您也可以使用更新策略 reload,当配置更改时 start/stop 蓝图容器。这将使用 pid "datasource" 加载配置(即,在 karaf 中,文件 etc/datasource.cfg):

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0">

  <cm:cm-properties id="myProps" persistent-id="datasource" update-strategy="reload"/>

  <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
      <property name="URL" value="${url}"/>
      <property name="user" value="${user}"/>
      <property name="password" value="${password}"/>
  </bean> 
</blueprint>

如果您想在不使用 ConfigurationAdmin 的情况下使用您的配置文件或动态重新加载您的包,那么您可以使用 ext 命名空间:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0">

    <ext:property-placeholder>
        <ext:location>file:config.properties</ext:location>
    </ext:property-placeholder>

    <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
          <property name="URL" value="${url}"/>
          <property name="user" value="${user}"/>
          <property name="password" value="${password}"/>
     </bean> 
</blueprint>