繁体   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