[英]Spring Boot Data JPA issues with DB/2 Configuration and JUnit
我正在开发一个想要使用spring-boot-data-jpa的项目。 本地(IDE)数据库可以是H2。 但是,对于所有服务器(dev / test / prod),我们不得不使用DB / 2(不幸的是!)。
我们似乎无法使用DB / 2。 我们得到这样的错误:
java.lang.IllegalStateException: Failed to load ApplicationContext....
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [...Repository] found for
dependency: expected at least 1 bean which qualifies as autowire candidate for this
dependency. Dependency annotations: {}
在运行单元测试时(JUnit)。 注意,空白{}注释。 从表面看来,这似乎是一个“明显”的问题。 无法在类路径中加载或找到存储库bean。 但是,当我们使用H2配置运行它时,它可以正常工作。
因此,下一个逻辑结论似乎是存储库配置(数据源等)未正确设置。 但是,我们可以在非Spring启动应用程序中使用相同的配置值,它工作正常!
所以我认为它可能是不同的类加载器或cglib代理不使用“真正的”实现类。 这样的问题:
但是,这些问题的正确答案无法解决问题。 所以这是我的帖子/问题。
如何找出这个问题的根本原因? 我试图在调试器中遍历代码(spring和我们的代码),但无法缩小原因。
所以我试过这个,我拿了示例spring boot jpa并修改它以使用DB / 2的特定属性,我可以得到同样的失败。 因此,我的配置似乎一定是错误的,或者Spring代码中存在错误。 我已经与其他人一起查看了配置,但我们没有看到问题。 我们在另一个(非春季启动应用程序)中尝试了此配置并且它可以工作。
这是一个补丁文件 ,显示原始jpa示例与修改后的配置更改之间的差异。 注意,我们删除了DB2服务器的详细信息。 希望这使得问题可以为任何人重现,然后可以帮助找出原因。
TIA,
斯科特
编辑1 ---在这里直接添加配置细节而不是补丁文件---
Java配置
@Configuration
@ComponentScan
@EnableAutoConfiguration
@PropertySource("classpath:application.properties")
public class SampleDataJpaApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleDataJpaApplication.class, args);
}
}
属性文件
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.hibernate.dialect: org.hibernate.dialect.DB2Dialect
#spring.jpa.database-platform: DB2Platform
spring.jpa.show-sql: true
spring.jpa.generate-ddl: true
spring.datasource.driverClassName: om.ibm.db2.jcc.DB2Driver
spring.datasource.url: jdbc:db2:someDB2-db:5000
spring.datasource.username: fakeuser
spring.datasource.password: fakepassword
spring.datasource.schema:schema-name
单元测试配置
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource("classpath:application.properties")
@ContextConfiguration(classes = SampleDataJpaApplication.class, loader = AnnotationConfigContextLoader.class)
public class CityRepositoryIntegrationTests {
//....
}
这应该与仅修改用户/密码/ jdbc URL的修补程序文件几乎相同,以隐藏环境的细节。
据我所知,JUnit设置不正确。 要让Boot正常工作,您有两种选择:
使用@SpringApplicationConfiguration
而不是@ContextConfiguration
Boot会相当多地配置ApplicationContext
引导过程,例如,启用默认配置文件。 使用自定义注释也会自动激活JUnit执行的内容。
自己配置@ContextConfiguration
和相应的侦听器
在您的情况下,除了注册的所有默认值之外,您至少需要ConfigFileApplicationContextInitializer
。
由于第二个选项相当繁琐,我强烈建议选择第一个选项,看看它在哪里引导你。
奥利弗,
我似乎@SpringApplicationConfiguration
使用how-to中定义的spring-boot-starter-test JAR找到注释@SpringApplicationConfiguration
。 我正在使用弹簧靴0.5.0.M6。 以下是我的Maven POM中的依赖项。 我在这里缺少什么?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
编辑 :在查看Spring Boot示例项目后,我发现了一种不同的方法来让Junit和Spring测试相互配合。 如果您可以像下面这样定义您的类,那么它应该与Oliver在其对选项1的回复中提到的相同。请注意,需要存在“loader”参数才能正确配置侦听器和支持bean。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyDataApplication.class, loader = SpringApplicationContextLoader.class)
public class MyServiceTest {
我在Spring Boot中遇到类似的集成测试问题,我可以确认@ user3166395解决方案。
这个配置对我有用:
import org.springframework.boot.test.SpringApplicationContextLoader;
import org.springframework.test.context.ContextConfiguration;
...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyDataApplication.class, loader = SpringApplicationContextLoader.class)
public class MyServiceTest {
@Autowired
CityRepository cityRepository;
}
这需要测试依赖性:
org.springframework.boot:spring-boot-starter-test:0.5.0.M7
我无法使用SpringApplicationConfiguration
实现工作测试
嘿伙计们晚了一点,但要清楚你的错误(因为我复制了它,我也有同样的情况):
spring.datasource.driverClassName: **om**.ibm.db2.jcc.DB2Driver
应该
spring.datasource.driverClassName: **com**.ibm.db2.jcc.DB2Driver
到目前为止,这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.