在多模块springboot项目中无法检索spring应用程序上下文/找不到EntityManagerFactory

时间:2019-09-06 10:52:35

标签: java spring-boot spring-boot-test component-scan spring-boot-configuration

我有一个多模块springboot项目。这是架构: enter image description here

@SpringBootApplication注释的类在顶部模块(webservice)中。当我在测试类中使用@SpringBootTest从顶层模块运行集成测试时,它可以正常工作。

但是现在我想从业务模块运行集成测试。 @SpringBootTest仅不再起作用,因为在业务模块中找不到配置类。因此,我在业务模块中创建了一个配置类:

package com.berthoud.p7.webserviceapp.business.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootConfiguration
@ComponentScan(basePackages = "com.berthoud.p7")
public class TestContextConfiguration {
}

在测试类中,我指向了这个配置类,如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestContextConfiguration.class)
public class BookResearchIT {
//my tests...
}

这样做,我希望Spring将在com.berthoud.p7包及其子文件夹中声明的所有bean添加到上下文中。确实,当我在测试类中自动装配Spring bean时,现在看起来还不错(IntelliJ不再告诉我们@autowired bean无法自动装配): enter image description here

但是,尽管如此,当我运行测试时,Spring无法加载应用程序上下文:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field bookReferenceDAO in com.berthoud.p7.webserviceapp.business.BookResearchManager required a bean of type 'com.berthoud.p7.webserviceapp.consumer.contract.BookReferenceDAO' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.berthoud.p7.webserviceapp.consumer.contract.BookReferenceDAO' in your configuration.

我不明白。这是我声明有关豆子的方法:

package com.berthoud.p7.webserviceapp.business;

@Service
public class BookResearchManager {

    @Autowired
    BookReferenceDAO bookReferenceDAO;

    @Autowired
    LibrairyDAO librairyDAO;

    @Autowired
    BookDAO bookDAO;
package com.berthoud.p7.webserviceapp.consumer.contract;

public interface BookReferenceDAO {
// method signatures
}
package com.berthoud.p7.webserviceapp.consumer.repositories.SpringDataJPA;

public interface BookReferenceRepository extends CrudRepository<BookReference, Integer>, BookReferenceDAO {

我做错了什么?

编辑::像这样更改了我的配置类后:

package com.berthoud.p7.webserviceapp.business.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@SpringBootConfiguration
@ComponentScan(basePackages = "com.berthoud.p7.webserviceapp")
@EnableJpaRepositories(basePackages = "com.berthoud.p7.webserviceapp.consumer")
@EntityScan(basePackages = "com.berthoud.p7.webserviceapp")
public class TestContextConfiguration {
}

我现在有另一个错误:

Field bookReferenceDAO in com.berthoud.p7.webserviceapp.business.BookResearchManager required a bean named 'entityManagerFactory' that could not be found.

0 个答案:

没有答案