Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available

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:

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)

@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
like image 754
Moazzam Avatar asked Jul 20 '17 11:07

Moazzam


1 Answers

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.

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

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.

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

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})

like image 109
ryan2049 Avatar answered Oct 10 '22 14:10

ryan2049