簡體   English   中英

NoSuchBeanDefinitionException:沒有符合類型的合格bean <package> &#39;可用:至少有1個符合自動裝配候選條件的bean

[英]NoSuchBeanDefinitionException: No qualifying bean of type '<package>' available: expected at least 1 bean which qualifies as autowire candidate

我有一個Spring Boot應用程序,正在編寫一個集成測試類。 運行測試類時,出現以下異常

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.whot.dao.HotspotDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1466) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:589) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]

我已經搜索了一段時間,並尋找了可能導致這種情況的線索,但我無法弄清這種情況的發生原因。 幫助將不勝感激。 我已經包含了一些我的代碼的細節。

 <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>${spring.boot.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>${spring.boot.version}</version>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${psql.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

    </dependencies>

我的Application.Java類中有這個

package com.whot;

@SpringBootApplication
@ComponentScan(basePackages = {"com.whot.controller"})
@EntityScan(basePackages = {"com.whot.entity"})
@EnableJpaRepositories(basePackages = {"com.whot.repository", "com.whot.dao"})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

在我的整合課程中

package com.whot.dao;


@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class HotspotDaoIntegrationTest {

    @Autowired
    private HotspotDao hotspotDao;

    @Autowired
    private  TestEntityManager entityManager;

    private  String hotspotName = "Mavericks";


    @Test
    public void givenHotspotNameReturnHotspotSuccessfully(){
        Address address = new Address("Allen Mgba Crescent", 30L);
        entityManager.persist(address);

        Hotspot hotspot = new Hotspot(hotspotName);
        entityManager.persist(hotspot);
        entityManager.flush();

        HotspotLocation hsLocation = new HotspotLocation(address);
        hsLocation.setHotspotId(hotspot.getId());
        entityManager.persist(hsLocation);
        hotspot.getHotspotLocations().add(hsLocation);

        HotspotDTO hotspotDto =((ArrayList<HotspotDTO>) hotspotDao.getHotspotByName(hotspotName)).get(0);
        assertEquals(hotspotDto.getId(), hotspot.getId());
        assertEquals(hotspotDto.getName(), hotspot.getName());
    }
}

還有這里的班級

package com.whot.dao;


@Component
public class HotspotDao {

    @Autowired
    HotspotRespository hsRepository;

    public Collection<HotspotDTO> getHotspotByName(String hotspotName) {
       Collection <HotspotDTO> hotspots = getHotspotsByName(Arrays.asList(hotspotName));
       return hotspots;
    }

    public Collection<HotspotDTO> getHotspotsByName(Collection <String> names) {
        Collection<Hotspot> hotspots =  hsRepository.findHotspotsByName(names);
        Collection<HotspotDTO> result = new ArrayList<>();
        for(Hotspot hotspot: hotspots){
            result.add(new HotspotDTO().toHotspotDto(hotspot));
        }
        return result;
    }
}

Spring Boot默認掃描將使用啟動應用程序類來啟動相同的軟件包和子打包程序。

如果其他注釋不在同一軟件包中,則它將無法掃描,解決方案是在啟動應用程序類上使用@ComponentScan注釋,聲明軟件包掃描路徑。 喜歡...

@ComponentScan(basePackages={"com.exp.package1","com.exp.package2"})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM