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;
}
}
類'HotspotDao'是在包'com.whot.dao',這是不下部件掃描('@ComponentScan(basePackages = { 「com.whot.controller」})' )。 – manish
@manish這基本上是一個答案,而不是一個評論。 – Wingie