在 Spring Boot 中禁用所有与数据库相关的自动配置 [英] Disable all Database related auto configuration in Spring Boot

查看:36
本文介绍了在 Spring Boot 中禁用所有与数据库相关的自动配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Boot 开发两个应用程序,一个用作服务器,另一个用作客户端应用程序.但是,它们都是同一个应用程序,根据活动配置文件的不同功能不同.我正在使用 Spring Boot 的自动配置功能来配置我的应用程序.

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.

我想在客户端应用程序上禁用所有与数据库相关的自动配置,因为它不需要数据库连接.应用程序不应尝试与数据库建立连接,也不应尝试使用任何 Spring Data 或 Hibernate 功能.数据库自动配置的启用或禁用应有条件并基于应用的活动配置文件.

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.

我可以通过为各自的配置文件创建两个不同的 application.properties 文件来实现这一点吗?

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?

推荐答案

我做类似事情的方式是:

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).

最后一步是从主 spring 引导类禁用自动配置:

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);
    }
}

将:@SpringBootApplication 改为:

@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

希望有帮助

这篇关于在 Spring Boot 中禁用所有与数据库相关的自动配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆