57

Is it possible to create a spring-boot application that has NO datasource? In my case i just need a simple REST app but it seems on start up that there is an attempt to auto initialise a datasource

My pom.xml is

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>a.b.c.d.test</groupId>
    <artifactId>rest-customer-builder</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>rest-customer-builder</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
        <!--<relativePath/> lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

My application.properties is

server.contextPath=/rest-customer-builder
server.port=9090

When i run

mvn clean install spring-boot:run

i see this error

015-08-18 14:54:31.870  INFO 12530 --- [lication.main()] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2015-08-18 14:54:31.903  WARN 12530 --- [lication.main()] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/geronimo/osgi/locator/ProviderLocator
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:199)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:615)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.bearingpoint.eircom.test.rest.customerbuilder.RestCustomerBuilderApplication.main(RestCustomerBuilderApplication.java:12)
    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.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/apache/geronimo/osgi/locator/ProviderLocator
    at javax.validation.Validation$DefaultValidationProviderResolver.getValidationProviders(Validation.java:209)
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:173)
    at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:223)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor$Jsr303ValidatorFactory.run(ConfigurationPropertiesBindingPostProcessor.java:381)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.afterPropertiesSet(ConfigurationPropertiesBindingPostProcessor.java:174)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.geronimo.osgi.locator.ProviderLocator
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
emeraldjava
  • 10,894
  • 26
  • 97
  • 170
  • Yes it's possible. Can you please show your pom.xml file or the gradle equivalent – reto Aug 18 '15 at 14:06
  • 2
    You must have more on the classpath than you've listed in the pom. `Jsr303ValidatorFactory` will only run if you've got the various `javax.validation` classes on the classpath. It also looks like you've got something on the classpath that contains metadata pointing to `org/apache/geronimo/osgi/locator/ProviderLocator` – Andy Wilkinson Aug 18 '15 at 15:27
  • 1
    Possible duplicate of [Disable all Database related auto configuration in Spring Boot](https://stackoverflow.com/questions/36387265/disable-all-database-related-auto-configuration-in-spring-boot) – 1615903 Jan 25 '18 at 09:45

7 Answers7

120

It is possible to run a spring boot application without datasource. You must disable the auto configuration for the datasource and may be for JPA also :

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

The exception you posted is something else, as written in the comments, you have something in the classpath that references the missing class of apache geronimo. So you must get rid of the code/jar that references geronimo or add geronimo to your dependencies.

  • 4
    This worked for me. I only had to exclude `DataSourceAutoConfiguration`. – Bernie Dec 16 '19 at 04:15
  • What about if I want to re-enable spring AutoConfiguration after I excluded it, because I've created a DataBase and I have a jdbc url connection? – Brayan Loayza Aug 28 '23 at 22:31
33

A better way to fix this point will be to remove the dependencies from your POM/Gradle configuration file and Spring Boot will not try to auto configure the DataSource.

Cristi B.
  • 651
  • 7
  • 17
  • 9
    e.g. if you're using Spring boot, remove "org.springframework.boot:spring-boot-starter-data-jpa" from your dependencies. – Michael Dotson Apr 07 '17 at 16:17
  • 1
    Removing org.springframework.boot:spring-boot-starter-data-jpa from the POM did the trick, thanks – Cécile Fecherolle Jul 02 '18 at 14:06
  • IMHO the right way regarding NO datasource - and for example just add spring-boot-starter-web for a main class extending SpringBootServletInitializer. – Gunnar Sep 01 '22 at 09:08
17

@Stefan +1 If you are using YAML file for configuration this is how it is

spring:
  profiles: dev
  autoconfigure:
    exclude:
    - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    - org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

It worked perfectly fine for me.

ROCKY
  • 1,763
  • 1
  • 21
  • 25
5

I had this issue, Once I have removed below dependency it started working.

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
Allwin
  • 2,060
  • 1
  • 17
  • 16
2

as for Spring Boot current version (2.5.4) you can put it on the @SpringBootApplication annotation like that:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class YourApplication {
public static void main(String[] args) {
    SpringApplication.run(YourApplication.class, args);
}
2

Basically I did the same thing as @Rocky wrote above in .properties file and it works.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
RuMcaizS
  • 33
  • 3
-5

Provide spring.datasource.url in the application.properties file. It worked for me. Example: spring.datasource.url=jdbc:mysql://localhost/Test_DB