Disable all Database related auto configuration in Spring Boot

202,161

Solution 1

The way I would do similar thing is:

@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@Profile ("client_app_profile_name")
public class ClientAppConfiguration {
    //it can be left blank
}

Write similar one for the server app (without excludes).

Last step is to disable Auto Configuration from main spring boot class:

@SpringBootApplication
public class SomeApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SomeApplication.class);
    }

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SomeApplication.class);
    }
}

Change: @SpringBootApplication into:

@Configuration 
@ComponentScan

This should do the job. Now, the dependencies that I excluded in the example might be incomplete. They were enough for me, but im not sure if its all to completely disable database related libraries. Check the list below to be sure:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#auto-configuration-classes

Hope that helps

Solution 2

For disabling all the database related autoconfiguration and exit from:

Cannot determine embedded database driver class for database type NONE

1. Using annotation:

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

    public static void main(String[] args) {
        SpringApplication.run(PayPalApplication.class, args);
    }
}

2. Using Application.properties:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

Solution 3

Seems like you just forgot the comma to separate the classes. So based on your configuration the following will work:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
    org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
    org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration

Alternatively you could also define it as follow:

spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.autoconfigure.exclude[2]=org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
spring.autoconfigure.exclude[3]=org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration

Solution 4

There's a way to exclude specific auto-configuration classes using @SpringBootApplication annotation.

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

@SpringBootApplication#exclude attribute is an alias for @EnableAutoConfiguration#exclude attribute and I find it rather handy and useful.
I added @Import(MyPersistenceConfiguration.class) to the example to demonstrate how you can apply your custom database configuration.

Solution 5

Way out for me was to add

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

annotation to class running Spring boot (marked with `@SpringBootApplication).

Finally, it looks like:

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

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
 }
Share:
202,161

Related videos on Youtube

yuva
Author by

yuva

Updated on March 02, 2022

Comments

  • yuva
    yuva about 2 years

    I am using Spring Boot to develop two applications, one serves as the server and other one is a client app. However, both of them are the same app that function differently based on the active profile. I am using auto configuration feature of Spring Boot to configure my applications.

    I want to disable all the database related auto configuration on client app, since it won't be requiring database connection. Application should not try to establish connection with the database, nor try to use any of the Spring Data or Hibernate features. The enabling or disabling of the database auto configuration should be conditional and based on the active profile of the app.

    Can I achieve this by creating two different application.properties files for respective profiles?

    I tried adding this to my properties file,

    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration\
      org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration\
    org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration\
      org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration
    

    But, the application still tries to connect to the database on start. Are those exclusions sufficient for achieving my requirement?

    • Rahul Sharma
      Rahul Sharma about 8 years
      This might help.
    • luboskrnac
      luboskrnac about 8 years
      Can you disclose your code/configuration?
    • Ali Dehghani
      Ali Dehghani about 8 years
      You can also use your build tool profiles and add the data related dependencies only on one of your profiles. If your package your app using the other profile, since it hasn't the required starter packages present on the classpath, it won't be auto-configured
  • Abhijit Sarkar
    Abhijit Sarkar over 6 years
    @SpringBootApplication has an exclude property, no need for ClientAppConfiguration.
  • patrykos91
    patrykos91 over 6 years
    Can you do that exclude conditional based on active profile without using ClientAppConfiguration?
  • Abhijit Sarkar
    Abhijit Sarkar over 6 years
    Yes. You'd exclude in the @SpringBootApplication, and then in the specific package, create a @Configuration class which does an @Import of the relevant classes and is dependent on @Profile or @Conditional. That way, you can test each application layer without the autoconfig leaking all over the app. Wanna test DB? Just scan the DB package, configure a mock DB, and you're good to go.
  • cvnew
    cvnew almost 6 years
    This is not a solution to the problem discussed. The issue is not about removing the JPA support from the application all together but rather enable/disable it based on a condition (such as a Spring profile) - without changing the code or Maven project configuration. You were getting the data source related error because, apparently, you forgot to define and activate the Spring profile that would load the "no-datasource" configuration instead of loading the DS/JPA-related classes. The JPA libraries should still remain in the distribution.
  • Tadele Ayelegn
    Tadele Ayelegn almost 6 years
    I do not think you read my post completely. On the last line i am suggesting it may be similiar issue but not saying that is the answer
  • Joshua Davis
    Joshua Davis over 5 years
    Thanks! This is the most modern answer. Linked to it here: konstructcomputers.blogspot.com/2018/10/…
  • Gustavo Rodrigues
    Gustavo Rodrigues about 4 years
    the use of Application.properties with Spring Boot 2+ is preferable over the annotation.
  • Betlista
    Betlista about 4 years
    @GustavoRodrigues can you share some documentation to support your statement? Thanks!
  • ankur pramanik
    ankur pramanik almost 4 years
    Can you tell me how to put a logger inside WithoutDB and WithDB class so that when I start the application print somemessage. Thank you
  • Andrew Alcock
    Andrew Alcock over 3 years
    @Betlista This is because the change that disables the DataSource autoconfiguration can occur in the version of application.properties you use for development. The production application.properties defines the DataSource. Thus the code is identical in development and production.
  • Stefan Falk
    Stefan Falk over 3 years
    I am using flyway.. My server fails because it's not able to initialize flyway because of the missing configuration. I tried to add FlywayAutoConfiguration.FlywayConfiguration.class but it's not working. Any ideas? ^^
  • Simon Logic
    Simon Logic about 3 years
    @GustavoRodrigues you can't disable with property because DataSourceAutoconfigurationCondition is triggered on DataSource class available on classpath.