我正在使用 Spring Boot 启动骆驼路线,该路线使用 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);
}
数据源配置.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 在 Spring Boot 中将“DataSource”注入 Registry。
?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