简体   繁体   中英

Apache Camel - Read JDBC dataSource properties from properties file using spring

I need to load datasource properties from properties file

db.properties:

url = my_url
user = user_name
password = user_pass

this is dataSource (camelcontext.xml):

I'm trying like this, it is not working.

<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> 

My routes are implemented in java dsl.

url = my_url属性名称=“ URL” value =“ $ {url}”

When using the expression language like ${...} you have to reference the keys of your property file, not the values. You propably want to write s.th. like

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

In order to use Camel Properties in Spring XML, you have to add the following Spring bean with the ID 'properties'

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

OR (after your comment please try this):

<?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"/> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM