在Java项目中,我使用的是Sprig Boot 1.5.3.RELEASE。它连接两个数据库,即 MongoDB和Microsoft SQLServer 。当我使用 spring-boot:run 目标运行它时,它运行正常。但是,当我尝试使用 package 目标运行它时,测试用例会报告以下错误,尽管这些测试用例未连接到SQL Server数据库:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
.....
.....
MediationTest.java(包含生成错误的测试用例的Java类)
@RunWith(SpringRunner.class)
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
@SpringBootTest(classes = { Application.class })
public class MediationTest {
@Autowired
private SwiftFormat swiftFormat;
......................
......................
MsqlDbConfig.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "msqlEntityManagerFactory", transactionManagerRef = "msqlTransactionManager", basePackages = { "com.msql.data" })
public class MsqlDbConfig {
@Bean(name = "msqlDataSource")
@ConfigurationProperties(prefix = "msql.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "msqlEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean msqlEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("msqlDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("com.utils.msql.info")
.persistenceUnit("msql").build();
}
@Bean(name = "msqlTransactionManager")
public PlatformTransactionManager msqlTransactionManager(
@Qualifier("msqlEntityManagerFactory") EntityManagerFactory msqlEntityManagerFactory) {
return new JpaTransactionManager(msqlEntityManagerFactory);
}
}
application.properties
spring.data.mongodb.uri=mongodb://dev-abc-123:27017/db
msql.datasource.url=jdbc:sqlserver://ABC-SQL14-WXX;databaseName=dev
msql.datasource.username=dev
msql.datasource.password=*****
msql.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
msql.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.show-sql=true
答案 0 :(得分:2)
spring-boot:run 目标由spring-boot-maven-plugin项目中包含的Mojo定义。你可以在这里找到它。 https://github.com/spring-projects/spring-boot/blob/8e3baf3130220a331d540cb07e1aca263b721b38/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java
Form2.Onclose
范围设置为“测试”。这将包括类路径上每个阶段的依赖关系。看看这里的规范。 https://maven.apache.org/developers/mojo-api-specification.html
Maven提供的包目标不会在类路径中包含这些额外的依赖项,我相信这是导致问题的原因。
Spring Boot提供了一个 repackage 目标,该目标应该用于构建可执行的spring-boot应用程序。
然而,要获得更多关键点。我认为,如果您更新测试以排除其他类,则可能会解决您的问题。
requiresDependencyResolution