簡體   English   中英

Apache Camel-從文件讀取JDBC數據源屬性

[英]Apache Camel - Read JDBC dataSource properties from file

我正在使用Apache Camel,我嘗試從此文件加載數據源屬性

config.properties:

url = my_url
user = user_name
password = user_pass

這是dataSource( 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中讀取值並將其插入到dataSource屬性中?

您談論的是blueprint.xml和駱駝,所以我假設您位於像Karaf / ServiceMix這樣的osgi容器中,並且您正在使用Aries Blueprint。

然后,您可以使用cm命名空間和一個property-placeholder 如果使用駱駝並希望動態地重新加載屬性,則也可以使用更新策略reload ,該更新策略在配置更改時啟動/停止藍圖容器。 這將使用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>

根據代碼,我假設您可能使用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> 

請檢查示例以了解詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM