2015-10-13 98 views
1

我有一個項目,並且使用spring-boot-jdbc-starter,它會自動爲我配置一個DataSource。 現在我將camel-spring-boot添加到項目中,並且我能夠成功地從RouteBuilder類型的豆類創建路線。 但是當我使用駱駝的SQL組件時,它找不到數據源。有沒有簡單的方法來添加Spring配置的數據源到CamelContext?在駱駝項目的示例中,他們使用spring xml進行數據源配置,但我正在尋找一種使用java配置的方法。這是我試過的:駱駝:使用由spring-boot配置的數據源

@Configuration 
public class SqlRouteBuilder extends RouteBuilder { 
    @Bean 
    public SqlComponent sqlComponent(DataSource dataSource) { 
    SqlComponent sqlComponent = new SqlComponent(); 
    sqlComponent.setDataSource(dataSource); 
    return sqlComponent; 
    } 

    @Override 
    public void configure() throws Exception { 
    from("sql:SELECT * FROM tasks WHERE STATUS NOT LIKE 'completed'") 
      .to("mock:sql"); 
    } 
} 
+1

我的壞,也沒有必要sqlComponent豆。由於所有Spring bean都可以通過CamelContext訪問,只需在sql端點末尾添加'?dataSource = dataSource',並且按預期工作 – MeysaM

+0

您應該發表該評論作爲答案並接受它:) –

回答

0

這是示例/示例代碼(Java DSL)。爲此,我使用

  • 春季啓動
  • H2嵌入式數據庫
  • 駱駝

在啓動春季啓動,創建表並加載數據。然後駱駝路線,運行「選擇」來拉取數據。

下面是代碼

public void configure() throws Exception { 

    from("timer://timer1?period=1000") 
    .setBody(constant("select * from Employee")) 
    .to("jdbc:dataSource") 
    .split().simple("${body}") 
    .log("process row ${body}") 

full example in github