0

I get a spring boot application and trying create a DataSource bean defined by Java configuration:

@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);
}

But when starting application, I got the following error:

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    You did not configure the datasource, it looks like. Or there is a problem in your config, it's hard to tell since you don't show us the neither the code nor the config. – M. Prokhorov Jun 19 '17 at 18:53
  • Hi @M. Prokhorov, this is my config code for the DataSource ` @Bean(name = "dsBD") @Primary public DataSource dsBD() { return DataSourceBuilder.create().url("jdbc:mysql://localhost:3306/database?autoReconnect=true").username("root") .password("mysql").driverClassName("com.mysql.jdbc.Driver").build(); } @Bean(name = "jdbcBD") @Autowired public JdbcTemplate jdbcBD(@Qualifier("dsBD") DataSource dsBD) { return new JdbcTemplate(dsBD); } ` – Jonathan Choy Rivera Jun 20 '17 at 00:40
  • That is fine and all, but can we have all of that in the question itself? – M. Prokhorov Jun 20 '17 at 06:13
  • I got error when run the app. – Jonathan Choy Rivera Jun 20 '17 at 15:59
  • Possible duplicate of [I am getting DataSource Not Supported when using DataSouceBuilder](https://stackoverflow.com/questions/34790924/i-am-getting-datasource-not-supported-when-using-datasoucebuilder) – Alex R Oct 11 '18 at 02:54

1 Answers1

7

In terms to use DataSourceBuilder you need to have one of the following on your classpath:

  1. COMMONS-DBCP
  2. TOMCAT-JDBC
  3. HIKARICP

Otherwise you will get an IllegalStateException for no supported type, as you get now.

Lazar Lazarov
  • 2,412
  • 4
  • 26
  • 35