Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - EnableAutoConfiguration with Exclude not working

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.

like image 451
Thiago Avatar asked Jan 26 '15 20:01

Thiago


2 Answers

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.

like image 154
Jason Avatar answered Oct 03 '22 14:10

Jason


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

like image 42
luboskrnac Avatar answered Oct 03 '22 16:10

luboskrnac