[英]Spring @ComponentScan annotation doesn't work
I have spring boot application sample with package layout: 我有包装布局的春季启动应用程序示例:
main:
-com.foo
Application.java
-com.foo.services
ItemService.java
ItemRepository.java
-com.foo.config
Configuration.java
test:
-com.foo.services
ItemServiceIngegrationTest.java
My integration test fails to run being not able to find ItemRepository bean if put 我的集成测试无法运行,如果放入则无法找到ItemRepository bean
@ComponentScan(basePackageClasses = { ItemService.class })
but works if I put 但如果我放,我会工作
@ComponentScan(basePackageClasses = { Application.class })
where is the trick ? 诀窍在哪里?
spec says : 规格说:
Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan.
可以指定basePackageClasses()或basePackages()(或其别名value())来定义要扫描的特定包。 If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.
如果未定义特定包,则将从声明此批注的类的包进行扫描。
@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 {
. . .
}
The javadoc says: javadoc说:
If specific packages are not defined , scanning will occur from the package of the class that declares this annotation.
如果未定义特定包 ,则将从声明此批注的类的包进行扫描。
So, it scans from the package that declares ComponentScan
unless a specific package class is defined. 因此,它会从声明
ComponentScan
的包中进行扫描,除非定义了特定的包类。 Can you put it in the Application
class? 你能把它放在
Application
类中吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.