[英]Apache Camel - Read JDBC dataSource properties from properties file using spring
[英]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.