在 Spring Boot 中禁用所有与数据库相关的自动配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36387265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable all Database related auto configuration in Spring Boot
提问by yuva
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 Boot 开发两个应用程序,一个用作服务器,另一个用作客户端应用程序。但是,它们都是同一个应用程序,根据活动配置文件的不同功能不同。我正在使用 Spring Boot 的自动配置功能来配置我的应用程序。
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.
我想在客户端应用程序上禁用所有与数据库相关的自动配置,因为它不需要数据库连接。应用程序不应尝试与数据库建立连接,也不应尝试使用任何 Spring Data 或 Hibernate 功能。数据库自动配置的启用或禁用应有条件并基于应用程序的活动配置文件。
Can I achieve this by creating two different application.properties files for respective profiles?
我可以通过为各自的配置文件创建两个不同的 application.properties 文件来实现这一点吗?
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?
但是,应用程序仍会在启动时尝试连接到数据库。这些排除是否足以满足我的要求?
采纳答案by patrykos91
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:
最后一步是从主 spring 引导类禁用自动配置:
@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:
变:@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:
这应该可以完成工作。现在,我在示例中排除的依赖项可能不完整。它们对我来说已经足够了,但我不确定是否完全禁用与数据库相关的库。检查下面的列表以确保:
Hope that helps
希望有帮助
回答by Jorge López
For disabling all the database related autoconfiguration and exit from:
禁用所有与数据库相关的自动配置并退出:
Cannot determine embedded database driver class for database type NONE
无法确定数据库类型 NONE 的嵌入式数据库驱动程序类
1. Using annotation:
1. 使用注解:
@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:
2. 使用 Application.properties:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
回答by Julien May
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
回答by naXa
There's a way to exclude specific auto-configuration classes using @SpringBootApplication
annotation.
有一种方法可以使用@SpringBootApplication
注释排除特定的自动配置类。
@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.
@SpringBootApplication#exclude
属性是属性的别名@EnableAutoConfiguration#exclude
,我觉得它相当方便和有用。
我添加@Import(MyPersistenceConfiguration.class)
到示例中以演示如何应用自定义数据库配置。
回答by ryzhman
Way out for me was to add
我的出路是添加
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
annotation to class running Spring boot (marked with `@SpringBootApplication).
运行 Spring Boot 的类的注释(用`@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);
}
}
回答by Dirk
Another way to control it via Profiles is this:
另一种通过配置文件控制它的方法是:
// note: no @SpringApplication annotation here
@Import(DatabaseConfig.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Configuration
@Import({DatabaseConfig.WithDB.class, DatabaseConfig.WithoutDB.class})
public class DatabaseConfig {
@Profile("!db")
@EnableAutoConfiguration(
exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
static class WithoutDB {
}
@Profile("db")
@EnableAutoConfiguration
static class WithDB {
}
}
回答by Sylvain
I had the same problem here, solved like this:
我在这里遇到了同样的问题,解决方法如下:
Just add another application-{yourprofile}.yml
where "yourprofile" could be "client".
只需添加另一个application-{yourprofile}.yml
“您的个人资料”可能是“客户”的地方。
In my case I just wanted to remove Redis in a Dev profile, so I added a application-dev.yml
next to the main application.yml
and it did the job.
就我而言,我只想在 Dev 配置文件中删除 Redis,所以我application-dev.yml
在主配置文件旁边添加了一个application.yml
,它完成了工作。
In this file I put:
在这个文件中,我放了:
spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
this should work with properties files as well.
这也适用于属性文件。
I like the fact that there is no need to change the application code to do that.
我喜欢这样一个事实,即无需更改应用程序代码即可做到这一点。
回答by Cássia Chagas
I add in myApp.java, after @SpringBootApplication
我在 @SpringBootApplication 之后添加 myApp.java
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
And changed
并且改变了
@SpringBootApplication=> @Configuration
@SpringBootApplication=> @Configuration
So, I have this in my main class (myApp.java)
所以,我在我的主类(myApp.java)中有这个
package br.com.company.project.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class SomeApplication {
public static void main(String[] args) {
SpringApplication.run(SomeApplication.class, args);
}
}
}
And work for me! =)
并对我来说有效!=)
回答by Tadele Ayelegn
I was getting this error even if I did all the solutions mentioned above.
即使我做了上面提到的所有解决方案,我也收到了这个错误。
by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfig ...
At some point when i look up the POM there was this dependency in it
在某些时候,当我查找 POM 时,其中存在这种依赖性
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
And the Pojo class had the following imports
Pojo 类具有以下导入
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
Which clearly shows the application was expecting a datasource.
这清楚地表明应用程序需要一个数据源。
What I did was I removed the JPA dependency from pom and replaced the imports for the pojo with the following once
我所做的是从 pom 中删除了 JPA 依赖项,并用以下内容替换了 pojo 的导入
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
Finally I got SUCCESSFUL build. Check it out you might have run into the same problem
最后我得到了成功的构建。看看你可能遇到了同样的问题