[英]Apache Camel - Read JDBC dataSource properties from file
i'm using Apache Camel and i try to load datasource properties from this file 我正在使用Apache Camel,我尝试从此文件加载数据源属性
config.properties: config.properties:
url = my_url
user = user_name
password = user_pass
this is dataSource ( blueprint.xml ): 这是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>
How can i read values from config.properties and insert them into dataSource properties ? 我如何从config.properties中读取值并将其插入到dataSource属性中?
You talk about blueprint.xml, and camel, so I assume you are in an osgi container like Karaf/ServiceMix, and you are using Aries Blueprint. 您谈论的是blueprint.xml和骆驼,所以我假设您位于像Karaf / ServiceMix这样的osgi容器中,并且您正在使用Aries Blueprint。
Then you can use the cm
namespace and a property-placeholder
. 然后,您可以使用cm
命名空间和一个property-placeholder
。 If you use camel and want your properties to be dynamically reloaded, then you can use too an update strategy reload
, which start/stop the blueprint container when the configuration change. 如果使用骆驼并希望动态地重新加载属性,则也可以使用更新策略reload
,该更新策略在配置更改时启动/停止蓝图容器。 This will load the configuration with pid "datasource" (ie, in karaf, the file etc/datasource.cfg
) : 这将使用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>
If you want to use your configuration file without using ConfigurationAdmin or dynamically reload your bundle, then you can use the ext
namespace : 如果要使用配置文件而不使用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>
According to code I assume you use probably spring as container. 根据代码,我假设您可能使用spring作为容器。 General solution in spring is to use PropertyPlaceHolder, your configuration will look like this: 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.