Camel SQL - 在Spring Boot中将DataSource放到SimpleRegistry中

时间:2016-11-21 16:54:36

标签: apache-camel datasource camel-sql

我使用Spring Boot启动一个使用Camel-sql查询MySQL数据库并调用REST服务的camel路由。

application.properties

db.driver=com.mysql.jdbc.Driver
db.url=mysql://IP:PORT/abc
db.username=abc
db.password=pwd

Application.java

public static void main(String[] args) {
    SpringApplication.run(WlEventNotificationBatchApplication.class, args);

}

DataSourceConfig.java

public class DataSourceConfig {

    @Value("${db.driver}")
    public String dbDriver;

    @Value("${db.url}")
    public String dbUrl;

    @Value("${db.username}")
    public String dbUserName;

    @Value("${db.password}")
    public String dbPassword;
    @Bean("dataSource")
    public DataSource getConfig() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(dbDriver);
        dataSource.setUrl(dbUrl);
        dataSource.setUsername(dbUserName);
        dataSource.setPassword(dbPassword);

        return dataSource;
    }
}

WLRouteBuilder.java

@Component
public class WLRouteBuilder extends RouteBuilder {
    @Autowired
    private NotificationConfig notificationConfig;

    @Autowired
    private DataSource dataSource;

    @Override
    public void configure() throws Exception {

        from("direct:eventNotification")
                .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource="+dataSource)
                .process(new RowMapper())
                .log("${body}");

    }
}

我运行时看到以下错误,发现Camel无法在注册表中找到DataSource bean。我不太确定如何注入" DataSource"使用Java DSL在Spring Boot中注册表。

?dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource%40765367 due to: No bean could be found in the registry for: org.springframework.jdbc.datasource.DriverManagerDataSource@765367 of type: javax.sql.DataSource

1 个答案:

答案 0 :(得分:2)

它是Camel在uri中使用的bean的名称,您可以使用此处记录的#语法引用它:http://camel.apache.org/how-do-i-configure-endpoints.html(引用bean)

所以类似的东西

 .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource=#dataSource"

其中dataSource是创建DataSource的bean的名称,您可以给出另一个名称,例如

@Bean("myDataSource")

然后Camel SQL端点是

    .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource=#myDataSource"