首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Camel SQL -在Spring Boot中将DataSource放到SimpleRegistry中

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

Stack Overflow用户
提问于 2016-11-22 00:54:36
回答 1查看 4.2K关注 0票数 1

我正在使用Spring Boot启动一个camel路由,该路由使用Camel-sql查询MySQL DB并调用REST服务。

application.properties

代码语言:javascript
复制
db.driver=com.mysql.jdbc.Driver
db.url=mysql://IP:PORT/abc
db.username=abc
db.password=pwd

Application.java

代码语言:javascript
复制
public static void main(String[] args) {
    SpringApplication.run(WlEventNotificationBatchApplication.class, args);

}

DataSourceConfig.java

代码语言:javascript
复制
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

代码语言:javascript
复制
@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。我不太确定如何使用Java DSL将"DataSource“注入Spring Boot中的注册表。

代码语言:javascript
复制
?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
EN

回答 1

Stack Overflow用户

发布于 2016-11-22 05:26:26

它是Camel在uri中使用的bean的名称,在这里使用#语法引用它:http://camel.apache.org/how-do-i-configure-endpoints.html (引用bean)

所以有些东西是相似的

代码语言:javascript
复制
 .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource=#dataSource"

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

代码语言:javascript
复制
@Bean("myDataSource")

然后Camel SQL端点是

代码语言:javascript
复制
    .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource=#myDataSource"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40725368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档