springboot扫描组件_spring boot - 从组件扫描中排除bean不起作用

在this spring boot project中,集成测试创建一个spring应用程序上下文,该上下文应创建所有必需的bean,除了那些带有 @ExcludeFromTests 注释的bean

@RunWith(SpringRunner.class)

@ActiveProfiles("test")

@SpringBootTest(classes=IntegrationTestApplication.class,

webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class IntegrationTest_CartController

{

/*....*/

}

测试应用加载 @Configuration 类

@Profile("test")

@Configuration

@ComponentScan(basePackages = {"com.example.demo", "com.example.demo.*"},

excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,

value = ExcludeFromTests.class))

@EnableJpaRepositories("com.example.demo.dao")

@EntityScan("com.example.demo.resource")

@EnableAutoConfiguration

public class TestDemoAppConfig

{

private static final Logger LOG = LoggerFactory.getLogger(TestDemoAppConfig.class);

public TestDemoAppConfig()

{

// I can see this being printed out

LOG.info("_________________________________");

LOG.info("Instantiating TestDemoAppConfig");

LOG.info("_________________________________");

}

}

也就是说,它会在 com.example.demo 包中加载bean,但应该忽略那些用 @ExcludeFromTests 注释的bean,例如

@Component

@ExcludeFromTests

public class ItemDaoInit

{

private static final Logger LOG = LoggerFactory.getLogger(ItemDaoInit.class);

public ItemDaoInit(@Autowired ItemDao dao)

{

init(dao);

}

private void init(ItemDao dao)

{

LOG.info(" ------------ item dao preprocessor called"); // I shouldn't see this when I run a test as this bean shouldnt be created

/* preprocess entries in dao */

}

}

当我运行应用程序时,我可以看到正确的 @Configuration 已加载但仍然, ItemDaoInit bean也被创建

""2018-02-28 11:54:28 [main] INFO com.example.demo.dao.ItemDaoInit - ------------ item dao preprocessor called

为什么创建这个bean虽然它带有我明确排除在组件扫描之外的注释?

EDIT

顺便说一句,如果我从上面的测试配置类中排除 @EnableAutoConfiguration ,问题仍然存在

EDIT 2

这是 IntegrationTestApplication . 它只是扫描 @Configuration 类

@SpringBootApplication

@ComponentScan(basePackageClasses= {TestDemoAppConfig.class})

public class IntegrationTestApplication

{

public static void main(String[] args) {

SpringApplication.run(IntegrationTestApplication.class, args);

}

}

谢谢您的帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值