【问题标题】:Apache Camel - Read JDBC dataSource properties from properties file using springApache Camel - 使用 spring 从属性文件中读取 JDBC 数据源属性
【发布时间】:2018-03-18 14:23:08
【问题描述】:

我需要从属性文件中加载数据源属性

db.properties:

url = my_url
user = user_name
password = user_pass

这是数据源(camelcontext.xml):

我正在尝试这样,它不起作用。

<bean 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:db.properties"/>  </bean>
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
  <property name="URL" value="${url}"/>
  <property name="user" value="${user}"/>
  <property name="password" value="${pasword}"/>
</bean> 

我的路由是用 java dsl 实现的。

【问题讨论】:

标签: java spring apache-camel


【解决方案1】:

为了在 Spring XML 中使用 Camel 属性,您必须添加以下带有 ID 'properties' 的 Spring bean

<bean id="properties"
    class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="location" value="classpath:db.properties"/>
</bean>

或者(在您发表评论后,请尝试此操作):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

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

    <context:property-placeholder properties-ref="properties"/>
    <util:properties id="properties" location="classpath:db.properties"/> 

【讨论】:

  • 谢谢Themis,还是不行,如果有例子或者链接就好了。
  • 请尝试新的。
【解决方案2】:

url = my_url 属性名称="URL" 值="${url}"

【讨论】:

    【解决方案3】:

    当使用 ${...} 这样的表达式语言时,您必须引用属性文件的键,而不是值。你可能想写s.th。喜欢

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

    【讨论】:

    • 感谢回复,使用 ${...} 我无法从属性文件中读取属性来创建数据源
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多