Java Spring Boot - EnableAutoConfiguration with Exclude 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28158094/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:42:01  来源:igfitidea点击:

Spring Boot - EnableAutoConfiguration with Exclude not working

javaspringspring-boot

提问by Thiago

I am using the latest spring boot version and I am trying to setup an application but I want to disable the DataSource configuration. My configuration class looks like this:

我正在使用最新的 Spring Boot 版本,我正在尝试设置一个应用程序,但我想禁用 DataSource 配置。我的配置类如下所示:

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class ApiApplicationConfig { }

but when I run the application, I am getting the following stacktrace:

但是当我运行应用程序时,我得到以下堆栈跟踪:

Caused by: org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:137)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:116)
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.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 31 more

Am I missing anything in my configuration to completely disable datasource configuration? I will be manually setting up a DataSource, so I dont want spring to handle this for me.

我的配置中是否缺少任何内容以完全禁用数据源配置?我将手动设置一个数据源,所以我不希望 spring 为我处理这个。

回答by luboskrnac

When you manually configure your datasource, spring Boot will use your configuration and wouldn't try to initialize embedded datasource.

当您手动配置数据源时,spring Boot 将使用您的配置并且不会尝试初始化嵌入式数据源。

BTW, Spring boot by default uses these properties from application.properties to create datasource bean:

顺便说一句,Spring boot 默认使用 application.properties 中的这些属性来创建数据源 bean:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Take a look at this section of Spring Boot docs for more details about data source auto-configuration

有关数据源自动配置的更多详细信息,请查看Spring Boot 文档的这一部分

回答by Jason

This seems to be a weird situation where DataSourceAutoConfiguration.NonEmbeddedDataSourceConditionfinds a DataSource class loader, but no DataSource. We had this problem with spring-boot 1.2.2 while running an integration test.

这似乎是一个奇怪的情况,DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition找到了一个 DataSource 类加载器,但没有找到 DataSource。我们在运行集成测试时遇到了 spring-boot 1.2.2 的这个问题。

Anyway, we ran gradle dependenciesto find out what was pulling in tomcat-jdbc and ended up replacing our spring-boot-jdbc dependency with plain spring-jdbc. If you don't have tomcat-jdbc in your dependencies, it may help to set a breakpoint in DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition.getDataSourceClassLoader()to find out what driver it finds.

无论如何,我们跑去gradle dependencies找出是什么在 tomcat-jdbc 中拉动,最终用普通的 spring-jdbc 替换了我们的 spring-boot-jdbc 依赖项。如果您的依赖项中没有 tomcat-jdbc,则在其中设置断点DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition.getDataSourceClassLoader()以找出它找到的驱动程序可能会有所帮助。

回答by sambed pattanaik

@Configuration

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

By using this we can disable the spring boot embedded database configuration.

通过使用它,我们可以禁用 spring boot 嵌入式数据库配置。

回答by Andreas Guther

The only thing that helped my exclusion problem was to exclude the tomcat jdbc dependency from the spring configuration:

唯一帮助我排除问题的是从 spring 配置中排除 tomcat jdbc 依赖项:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

回答by dorony

I had an issue when using @Configuration, @EnableAutoConfigurationand @ComponentScanwhile trying to exclude specific configuration classes, the thing is it didn't work!

我在尝试排除特定配置类时使用@Configuration@EnableAutoConfiguration@ComponentScan时遇到问题,问题是它不起作用!

Eventually I solved the problem by using @SpringBootApplication, which according to Spring documentation does the same functionality as the three above in one annotation.

最终我通过使用@SpringBootApplication解决了这个问题,根据 Spring 文档,它在一个注释中完成了与上述三个相同的功能。

@SpringBootApplication(exclude= {Foo.class})
public class MySpringConfiguration {}

回答by Freelancer

Its because when you disable the datasource config, spring boot uses in-memory database which is not present in your classpath. You have to add in-memory database dependency in your classpath -

这是因为当您禁用数据源配置时,spring boot 使用类路径中不存在的内存数据库。您必须在类路径中添加内存数据库依赖项 -

<dependency>
     <groupId>com.h2database</groupId>
     <artifactId>h2</artifactId>
     <scope>test</scope>
</dependency>

This is the same issue occurs when you using @DataJpaTestfor testing.

这与您@DataJpaTest用于测试时发生的问题相同。