dcalap
dcalap

Reputation: 1072

Running Spring Boot without database fails

Im trying to configure a project without database and fails with the next error:

Caused by: java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType
at org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection.<clinit>(EmbeddedDatabaseConnection.java:49) ~[spring-boot-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.<init>(DataSourceProperties.java:155) ~[spring-boot-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_161]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_161]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 19 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_161]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_161]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_161]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_161]
... 26 common frames omitted

In my SpringBootApplication class I have the next configuration:

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

It doesn't make sense because is like trying to read a DataSourceProperties class in a No Database application.

I'm using gradle with the next dependencies:

compile "io.springfox:springfox-swagger2:${swaggerVersion}",
        "org.springframework.boot:spring-boot-starter-actuator",
        "org.springframework.cloud:spring-cloud-starter-config",
        "org.springframework.boot:spring-boot-starter-web",
      //"org.springframework:spring-jdbc",
        "io.fabric8:spring-cloud-starter-kubernetes:${springCloudKubernetes}",
        "ma.glasnost.orika:orika-core:${orikaVersion}",
        'org.projectlombok:lombok:1.16.20'

testCompile "org.springframework.boot:spring-boot-starter-test",
        'com.github.sbrannen:spring-test-junit5:1.0.2',
        'org.apiguardian:apiguardian-api:1.0.0',

        "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
testRuntime "org.junit.jupiter:junit-jupiter-engine:${junitVersion}",
        "org.junit.platform:junit-platform-launcher:1.0.2"

testAgent("org.jacoco:org.jacoco.agent:${jacocoVersion}:runtime")

If I add the "org.springframework:spring-jdbc" works but doesn't make sense for me to add a database lib for a no database project.

Any idea or clue? Thanks in advance.

Upvotes: 0

Views: 5166

Answers (2)

Igor
Igor

Reputation: 1640

I faced the same problem (using Spring-Boot 2.0.0.RELEASE): I have a Spring-Boot-package without a database. It solely serves static files.

In order to make it work (and not crash during database auto-configuration), I replaced the annotation @SpringBootApplication with the following two:

@SpringBootConfiguration
@EnableAutoConfiguration

This made the server start without a JPA-/JDBC-dependency and without any database-related configuration property inside the application.properties file.

Upvotes: 1

Juan Rada
Juan Rada

Reputation: 3766

Just try your shared dependency and I everything looks ok (application start with not issue), as expected there is not need to @EnableAutoConfiguration for excluding DataSourceAutoConfiguration as you do not have a DataSource class in the path (at least with those dependencies). So I believe

  1. You have problem refreshing dependencies in your system/IDE, try gradle clean build
  2. You are using wrong dependency combinations spring-cloud-starter-config for example 2.0.0.M6 with spring boot 1.5.x
  3. You have another dependency adding DataSource class to class path.
  4. You have in your resource file of resource server data source configuration properties.

Upvotes: 0

Related Questions