gpt4 book ai didi

java - 在 Spring Boot 中排除 Hibernate 不起作用

转载 作者:行者123 更新时间:2023-11-30 07:26:55 24 4
gpt4 key购买 nike

我正在尝试在没有 hibernate 的情况下启动 JUnit 测试。我的应用程序类:

@SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class,
DataSourceAutoConfiguration.class, JpaBaseConfiguration.class, WebMvcAutoConfiguration.class })
@ComponentScan("my.base.package")
public class TestContext {

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

}

正如你所看到的,我排除了越来越多的东西,但它总是同样的错误。我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestContext.class)
public class TestMail {
@Autowired
private Component c;

public void setC(Component c) {
this.c = c;
}

@Test
public void test() {
...
}

}

当我开始测试时,Hibernate 将被配置并失败:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "dev" are currently active).

因此,仅启动应用程序或作为测试上下文也会启动 hibernate 配置。我做错了什么?

编辑:仅具有:

@SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class}) 

在我尝试使用实际的 hibernate 功能之前不会抛出任何异常。然而它正确地构建了数据库......

编辑2:问题是一个启用了 @EnableJpaRepositories 的自定义配置类。此外,这似乎也是不可能的,因为使用 @Component 注释且具有 Autowiring 存储库的 Bean 将失败,因为无法创建 Bean。

最佳答案

尝试将 DataSourceTransactionManagerAutoConfiguration.class 添加到排除列表中。当我需要从自动配置中排除 hibernate 时,这对我有用。

您是否尝试过使用 --debug 开关启动测试?这将报告自动配置( https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html )。

关于java - 在 Spring Boot 中排除 Hibernate 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36697176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com