【发布时间】:2023-03-16 07:35:02
【问题描述】:
给定一个使用camel-archetype-blueprint 生成的非常简单的Karaf Camel 包,我想添加一个通过属性配置的数据源,而不是在blueprint.xml 中。
我尝试配置PropertiesComponent 并以各种方式访问MySQL 数据源的property 值内的属性,但似乎都不起作用。但是,在记录消息时,可以访问这些属性。
如何使用属性文件中的参数值配置数据源?
我特别需要它来为多个捆绑包使用相同的数据源配置并区分生产/测试环境。我考虑在构建期间使用 Maven 编写属性,具体取决于目标环境。还有其他关于如何解决此数据源问题的最佳实践吗?
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
<property name="location" value="classpath:config.properties" />
</bean>
<bean id="dataSourceMySQL" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
<property name="url" value="jdbc:mysql://127.0.0.1/test_database" />
<!-- This causes an error, as it tries to connect with
`${mysqlUser}`@`localhost` without any evaluation -->
<property name="user" value="${mysqlUser}" />
<property name="password" value="${mysqlPassword}" />
</bean>
<service interface="javax.sql.DataSource" ref="dataSourceMySQL">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/mysqlDatasource" />
</service-properties>
</service>
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSourceMySQL" />
</bean>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="messageQuery">
<from uri="sql:SELECT * FROM messages" />
<log message="The user property is: {{mysqlUser}}, the query result is: ${body}" />
</route>
</camelContext>
</blueprint>
只是为了概览,项目布局如下所示:
【问题讨论】:
-
您能否尝试将您的 config.properties 文件放在 src/main/resource 下的一个文件夹中
-
你不是需要放置你的属性文件的FOLDER子么。您正在使用 OSGI,所以这个文件夹可能已经被扫描,但如果它只是 Spring,您需要将文件夹放入该文件。
-
试试
-
也将 ${mysqlUser} 替换为 {{mysqlUser}}
标签: java apache-camel datasource apache-karaf blueprint