3

我有带有包布局的 Spring Boot 应用程序示例:

main:
-com.foo
     Application.java       
-com.foo.services
      ItemService.java
      ItemRepository.java    
-com.foo.config
     Configuration.java
test:
-com.foo.services 
     ItemServiceIngegrationTest.java

如果放置,我的集成测试无法运行无法找到 ItemRepository bean

  @ComponentScan(basePackageClasses = { ItemService.class })

但如果我把

  @ComponentScan(basePackageClasses = { Application.class })

诀窍在哪里?

规格 说:

可以指定 basePackageClasses() 或 basePackages()(或其别名 value())来定义要扫描的特定包。如果未定义特定的包,则会从声明此注解的类的包中进行扫描。

@EnableAutoConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Configuration.class })
public class ItemServiceIntegrationTest { 
    . . .
}

@org.springframework.context.annotation.Configuration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackageClasses = { ItemService.class })
public class Configuration extends AbstractMongoConfiguration {
   .  .  .
}   
4

1 回答 1

2

javadoc 说:

如果未定义特定包,则将从声明此注解的类的包中进行扫描。

因此,ComponentScan除非定义了特定的包类,否则它会从声明的包中进行扫描。你能把它放在Application课堂上吗?

于 2016-02-21T04:35:20.047 回答