没有'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder'类型的限定bean

时间:2022-09-11 16:52:59

In Java project, I am using Sprig Boot 1.5.3.RELEASE. It is connecting with two databases i.e. MongoDB and Microsoft SQLServer. When I run it with spring-boot:run goal, it works fine. However, when I try to run it with package goal then below error is reported by test cases despite the fact that those test cases are not connecting to SQL Server database:

在Java项目中,我使用的是Sprig Boot 1.5.3.RELEASE。它连接两个数据库,即MongoDB和Microsoft SQLServer。当我用spring-boot运行它:运行目标时,它运行正常。但是,当我尝试使用包目标运行它时,测试用例会报告以下错误,尽管这些测试用例未连接到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 class containing test cases generating above error)

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

1 个解决方案

#1


2  

The spring-boot:run goal is defined by the Mojo included within the spring-boot-maven-plugin project. You can find it here. 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.

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的。

The requiresDependencyResolution scope is set to Test. This will include the dependencies from each phase on the classpath. Take a look at the specification here. https://maven.apache.org/developers/mojo-api-specification.html

requiresDependencyResolution范围设置为Test。这将包括类路径上每个阶段的依赖关系。看看这里的规范。 https://maven.apache.org/developers/mojo-api-specification.html

The package goal provided by Maven wouldn't include these additional dependencies on the classpath and I believe that is the cause of your issues.

Maven提供的包目标不会在类路径中包含这些额外的依赖项,我相信这是导致问题的原因。

Spring Boot provides a repackage goal which is what should be used for building out executable spring-boot applications.

Spring Boot提供了一个重新打包目标,它应该用于构建可执行的spring-boot应用程序。

However, to get more to the point. I think if you update your test to exclude an additional class it might fix your problem.

但是,要获得更多关键点。我认为如果您更新测试以排除其他类,它可能会解决您的问题。

@DataMongoTest(excludeAutoConfiguration = {EmbeddedMongoAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

@DataMongoTest(excludeAutoConfiguration = {EmbeddedMongoAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

#1


2  

The spring-boot:run goal is defined by the Mojo included within the spring-boot-maven-plugin project. You can find it here. 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.

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的。

The requiresDependencyResolution scope is set to Test. This will include the dependencies from each phase on the classpath. Take a look at the specification here. https://maven.apache.org/developers/mojo-api-specification.html

requiresDependencyResolution范围设置为Test。这将包括类路径上每个阶段的依赖关系。看看这里的规范。 https://maven.apache.org/developers/mojo-api-specification.html

The package goal provided by Maven wouldn't include these additional dependencies on the classpath and I believe that is the cause of your issues.

Maven提供的包目标不会在类路径中包含这些额外的依赖项,我相信这是导致问题的原因。

Spring Boot provides a repackage goal which is what should be used for building out executable spring-boot applications.

Spring Boot提供了一个重新打包目标,它应该用于构建可执行的spring-boot应用程序。

However, to get more to the point. I think if you update your test to exclude an additional class it might fix your problem.

但是,要获得更多关键点。我认为如果您更新测试以排除其他类,它可能会解决您的问题。

@DataMongoTest(excludeAutoConfiguration = {EmbeddedMongoAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

@DataMongoTest(excludeAutoConfiguration = {EmbeddedMongoAutoConfiguration.class,HibernateJpaAutoConfiguration.class})