我正在使用 Spring Boot 启动 Camel 路由,该路由使用 Camel-sql 查询 MySQL DB 并调用 REST 服务。
应用程序属性
db.driver=com.mysql.jdbc.Driver
db.url=mysql://IP:PORT/abc
db.username=abc
db.password=pwd
应用程序.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。我不太确定如何使用 Java DSL 将“DataSource”注入(inject) 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
最佳答案
它是 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"
关于apache-camel - Camel SQL - 在 Spring Boot 中将 DataSource 放入 SimpleRegistry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40725368/