java 我在使用 DataSouceBuilder 时得到 DataSource Not Supported
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34790924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I am getting DataSource Not Supported when using DataSouceBuilder
提问by VydorScope
I am new to Spring-Batch (and Spring in general), and have been following on line documentation to teach myself what I need to do this task. I am trying to connect to a DB2 database.
我是 Spring-Batch(以及一般的 Spring)的新手,并且一直在关注在线文档以自学我需要做什么来完成这项任务。我正在尝试连接到 DB2 数据库。
If I declare the DB2 connection with XML like this:
如果我像这样用 XML 声明 DB2 连接:
<bean id="wcs_dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
<property name="url" value="jdbc:db2://127.0.0.1/DEV" />
<property name="username" value="user" />
<property name="password" value="pass5" />
</bean>
Then load it in my code like so:
然后将它加载到我的代码中,如下所示:
@Bean
public JdbcCursorItemReader<Product> databaseItemReader() {
ApplicationContext context =
new ClassPathXmlApplicationContext("context-datasource.xml");
DataSource dataSource = (DataSource) context.getBean("wcs_dataSource");
((ConfigurableApplicationContext)context).close();
JdbcCursorItemReader<Product> result = new JdbcCursorItemReader<Product>();
result.setDataSource(dataSource);
result.setSql(sqlString);
result.setRowMapper(new ProductRowMapper());
return result;
}
It works perfectly. How ever I would like to use the DataSourceBuilder like the examples show so ultimately I would like to get to :
它完美地工作。我多么想像示例所示那样使用 DataSourceBuilder 所以最终我想:
@ConfigurationProperties(prefix="DEV.datasource")
public DataSource Wcs_DataSource(){
return DataSourceBuilder.create().build();
}
But for some reason that does not work. I get
但由于某种原因,这是行不通的。我得到
Caused by: java.lang.IllegalStateException: No supported DataSource type found
引起:java.lang.IllegalStateException:找不到支持的数据源类型
I have also tried:
我也试过:
public DriverManagerDataSource dataSource() {
DataSourceBuilder DSBuilder = DataSourceBuilder.create();
DSBuilder.url("jdbc:db2://127.0.0.1/DEV");
DSBuilder.username("user");
DSBuilder.password("password");
DSBuilder.driverClassName("com.ibm.db2.jcc.DB2Driver");
DriverManagerDataSource result = (DriverManagerDataSource) DSBuilder.build();
return result;
}
And I get the same error. If I run it in the debugger, I can see that the error happens on the .build().
我得到了同样的错误。如果我在调试器中运行它,我可以看到错误发生在 .build() 上。
I am sure I am missing something easy, but I can not figure it out.
我确定我错过了一些简单的东西,但我无法弄清楚。
回答by VydorScope
M. Deinum answered it. I was missing commons-dbcp from my dependencies! I figured it was something easy like that.
M. Deinum 回答了它。我的依赖项中缺少 commons-dbcp!我想这是一件很容易的事情。
To use the DataSourceBuilder you need to have commons-dbcp, or tomcat-jdbcor hikaricpon your classpath else it won't work. I you don't have one of those you will get the message as you get.
要使用 DataSourceBuilder,您需要在类路径上安装commons-dbcp或 tomcat-jdbc或hikaricp,否则它将无法工作。我你没有你会得到的消息之一。
回答by Vincent
In my case, add spring-boot-starter-jdbcdependency works:
就我而言,添加spring-boot-starter-jdbc依赖项有效:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>