2016-12-26 70 views
0

我有一个弹簧启动应用程序,我正在编写一个集成测试类。 当我运行我的测试类,我得到下面的异常NoSuchBeanDefinitionException:没有可用的'<package>'类型的合格bean可用:预计至少有1个符合自动线候选资格的bean

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; 
    } 
} 
+0

类'HotspotDao'是在包'com.whot.dao',这是不下部件扫描('@ComponentScan(basePackages = { “com.whot.controller”})' )。 – manish

+0

@manish这基本上是一个答案,而不是一个评论。 – Wingie

回答

0

春季启动默认扫描将开始与启动应用程序类相同的包和子包。

如果其他注释不在同一个包中,将无法扫描,解决方案是在启动应用程序类中使用@ComponentScan注释,声明包扫描路径。像...

@ComponentScan(basePackages={"com.exp.package1","com.exp.package2"}) 
相关问题