2016-03-03 20 views
0

我在我的應用程序context.xml文件如下:bean名稱如何與組件相關聯?

<bean id="reportingDataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="org.postgresql.Driver"/> 
    <property name="url" value="jdbc:postgresql://192.168.1.1:5432/reports"/> 
    <property name="username" value="xxx"/> 
    <property name="password" value="xxx"/> 
    <property name="initialSize" value="3"/> 
    <property name="maxActive" value="10"/> 
</bean> 

<bean id="reportingSql" class="org.apache.camel.component.sql.SqlComponent"> 
    <property name="DataSource" ref="reportingDataSource"/> 
</bean> 

和我的路線(片段)是這樣的:

.to("reportingSql:insert into my_table (uuid, name, created_at, created_by) values ('a','namename', 1, 2)") 

清楚,我只是堅持我的腳趾在這裏水域。

這段代碼在/var/log/tomcat/whistler.log產生一個錯誤:

...due to: No component found with scheme: reportingSql 

如果我改變「reportingSql」在XML文件和路線只是「SQL」,我得到進一步 - 錯誤已經一去不復返了。不知何故,'sql'魔法?

如果是這樣,我想沒關係,但如果我想使用額外的數據源呢?我無法將它們都稱爲'sql'。

回答

1

組件通過ComponentResolver進行解析。默認情況下,DefaultComponentResolver使用以下策略:

  1. 查找駱駝註冊表中的組件。如果找到實例,則轉換爲Component
  2. 使用默認的FactoryFinder實例化新組件。這家工廠使用META-INF/services/org/apache/camel/component/

下注冊類在性能上換句話說,你想要做什麼是可能的:駱駝應該在註冊表檢查命名實例。然而,在你的例子中,你使用了一個spring文件來註冊beans,而java使用它來創建一個路由。

使用彈簧時,當您使用<camelContext../>時,會創建一個SpringCamelContext而不是DefaultCamelContext。這個上下文使用幾種服務,允許在Spring應用程序中更好地集成。其中一項服務是將Spring bean集成到駱駝註冊表中的ApplicationContextRegistry。這個註冊表允許使用在Spring註冊的bean作爲Component

因此,檢查您是否使用Spring上下文中的CamelContext。如果不是,或者您不能使用託管camelContext,則可以嘗試使用SpringCamelContextApplicationContextRegistry

參見:

相關問題