【问题标题】:Spring Boot - EnableAutoConfiguration with Exclude not workingSpring Boot - EnableAutoConfiguration 与排除不起作用
【发布时间】:2015-03-25 08:04:47
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 我猜你的应用程序中有另一个类用 EnableAutoConfiguration 注释并且没有排除
  • 您是否也在使用@SpingBootApplication 注释?添加到@SpingBootApplication 的排除列表对我有用。根据auto configuration documentation :“您需要通过将@EnableAutoConfiguration 或@SpringBootApplication 注释添加到您的@Configuration 类之一来选择加入自动配置。您应该只添加一个@EnableAutoConfiguration 注释。我们通常建议您添加将其添加到您的主要 @Configuration 类。”

标签: java spring spring-boot


【解决方案1】:

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

顺便说一句,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

看看this section of Spring Boot docs for more details about data source auto-configuration

【讨论】:

  • 我没有使用@-Bean 注解来配置我的数据源,我必须依靠另一种方法来创建我的数据源。当我的应用程序启动时,spring 上下文中应该没有数据源(这就是为什么我试图从@-EnableAutoConfiguration 中排除 DataSource),一旦我的工作(Scheduled annotation)运行,它将设置我的数据源并注入 spring上下文。
  • 嗯,很不寻常。 AFAIK 你唯一的选择是根本不使用@EnableAutoConfiguration
【解决方案2】:

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

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

【讨论】:

  • 当我在 maven pom.xml 中排除了 tomcat-jdb.jar 时,它工作正常。
【解决方案3】:
@Configuration

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

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

【讨论】:

    【解决方案4】:

    唯一对我的排除问题有帮助的就是从 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>
    

    【讨论】:

      【解决方案5】:

      在尝试排除特定配置类时,我在使用 @Configuration@EnableAutoConfiguration@ComponentScan 时遇到了问题,问题就是这样没用!

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

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

      【讨论】:

        【解决方案6】:

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

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

        当您使用@DataJpaTest 进行测试时,也会出现同样的问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-17
          • 2018-09-21
          • 2020-02-25
          • 2018-06-14
          相关资源
          最近更新 更多