16

I am using the latest spring boot version and I am trying to setup an application but I want to disable the DataSource configuration. My configuration class looks like this:

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class ApiApplicationConfig { }

but when I run the application, I am getting the following stacktrace:

Caused by: org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:137)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 31 more

Am I missing anything in my configuration to completely disable datasource configuration? I will be manually setting up a DataSource, so I dont want spring to handle this for me.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
Thiago
  • 5,152
  • 11
  • 35
  • 44
  • I'd guess that you've got another class in your app that's annotated with `EnableAutoConfiguration` and doesn't have the exclude – Andy Wilkinson Jan 27 '15 at 15:18
  • 2
    Are you using `@SpingBootApplication` annotation also by any chance? Adding to the exclude list on `@SpingBootApplication` worked for me. As per [auto configuration documentation](http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html) : "You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes. You should only ever add one @EnableAutoConfiguration annotation. We generally recommend that you add it to your primary @Configuration class." – Naymesh Mistry Aug 03 '15 at 23:45

6 Answers6

4

This seems to be a weird situation where DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition finds a DataSource class loader, but no DataSource. We had this problem with spring-boot 1.2.2 while running an integration test.

Anyway, we ran gradle dependencies to find out what was pulling in tomcat-jdbc and ended up replacing our spring-boot-jdbc dependency with plain spring-jdbc. If you don't have tomcat-jdbc in your dependencies, it may help to set a breakpoint in DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition.getDataSourceClassLoader() to find out what driver it finds.

Jason
  • 2,025
  • 2
  • 21
  • 13
3

When you manually configure your datasource, spring Boot will use your configuration and wouldn't try to initialize embedded datasource.

BTW, Spring boot by default uses these properties from application.properties to create datasource bean:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Take a look at this section of Spring Boot docs for more details about data source auto-configuration

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • I am not configuring my datasource using the @-Bean annotation, I have another method I have to rely on to create my datasource. When my application starts, there should be no data source in the spring context (thats why I am trying to exclude the DataSource from the @-EnableAutoConfiguration), once my job (Scheduled annotation) runs, it will setup my datasource and inject into spring context. – Thiago Jan 26 '15 at 21:23
  • Hmm, pretty unusual. AFAIK your only option is not to use @EnableAutoConfiguration at all – luboskrnac Jan 26 '15 at 21:25
3

The only thing that helped my exclusion problem was to exclude the tomcat jdbc dependency from the spring configuration:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
Andreas Guther
  • 422
  • 4
  • 7
1

I had an issue when using @Configuration, @EnableAutoConfiguration and @ComponentScan while trying to exclude specific configuration classes, the thing is it didn't work!

Eventually I solved the problem by using @SpringBootApplication, which according to Spring documentation does the same functionality as the three above in one annotation.

@SpringBootApplication(exclude= {Foo.class})
public class MySpringConfiguration {}
dorony
  • 1,008
  • 1
  • 14
  • 31
0
@Configuration

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

By using this we can disable the spring boot embedded database configuration.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
0

Its because when you disable the datasource config, spring boot uses in-memory database which is not present in your classpath. You have to add in-memory database dependency in your classpath -

<dependency>
     <groupId>com.h2database</groupId>
     <artifactId>h2</artifactId>
     <scope>test</scope>
</dependency>

This is the same issue occurs when you using @DataJpaTest for testing.

Derrick
  • 3,669
  • 5
  • 35
  • 50