创建DataSource时出错

时间:2017-06-19 16:49:45

标签: spring-boot spring-jdbc

我得到一个spring boot应用程序,并尝试创建一个由Java配置定义的DataSource bean:

@Bean(name = "dsBD") @Primary
public DataSource dsBD() {
  return DataSourceBuilder.create()
    .url("jdbc:mysql://localhost:3306/‌​database?autoReconne‌​ct=true")
    .username("‌​root")
    .password("mysql")
    .driverClassName("com.mysql.jdbc.Drive‌​r")
    .build();
}

@Bean(name = "jdbcBD") @Autowired
public JdbcTemplate jdbcBD(@Qualifier("dsBD") DataSource dsBD) {
  return new JdbcTemplate(dsBD);
}

但是在启动应用程序时,我收到以下错误:

Caused by: java.lang.IllegalStateException: No supported DataSource type found
    at org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder.getType(DataSourceBuilder.java:138) ~[spring-boot-autoconfigure-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder.build(DataSourceBuilder.java:69) ~[spring-boot-autoconfigure-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at com.package.DatabaseConfig.dsBD(DatabaseConfig.java:29) ~[classes/:na]
    at com.package.DatabaseConfig$$EnhancerBySpringCGLIB$$ac56d75a.CGLIB$dsBD$0(<generated>) ~[classes/:na]
    at com.package.DatabaseConfig$$EnhancerBySpringCGLIB$$ac56d75a$$FastClassBySpringCGLIB$$159befb3.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at com.package.DatabaseConfig$$EnhancerBySpringCGLIB$$ac56d75a.dsBD(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    ... 46 common frames omitted

1 个答案:

答案 0 :(得分:5)

就使用DataSourceBuilder而言,您需要在类路径中使用以下其中一项:

  1. 公地DBCP
  2. TOMCAT-JDBC
  3. HIKARICP
  4. 否则,您现在可以获得IllegalStateException没有支持的类型。