@ComponentScan不起作用

时间:2016-08-02 02:14:50

标签: spring

我有以下代码:

Bean的实现:

package my.persist.services;
@Component
public class MyService{
}

测试:

package my.persist.services;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={"other configuration classes", my.persist.services.MyService.class})
@ComponentScan(basePackageClasses = {my.persist.services.DummyPlaceHolder.class})
public class MyServiceTest extends AbstractJUnit4SpringContextTests {
        @Autowired
        MyService service;
}

    当我从@ContextConfiguration中删除“ my.persist.services.MyService.class ”时,编译器会说“无法自动装配,没有找到...的bean”,它似乎是{{ 1}}没有效果?有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

对基础包的通配符扫描是否有效,而不是对各个类进行组件扫描?

@ComponentScan(basePackages = {"my.persist.services.*"})

您可以在测试中排除某些内容,例如,如果要在测试中过滤掉实际的实现,可以执行以下操作:

@ComponentScan(basePackages = {"my.persist.services.*"}, excludeFilters={
          @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=MyService.class)})