@SpringBootTest为何扫描测试包无@Configuration的同名类?
Spring Boot测试中未标注@Configuration的类被加载的问题
main目录下的Kotlin代码
package com.example.demo // in main folder @SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @Configuration("show") class Show { init { System.err.println("[main] this is Test#init") } @Bean("map") fun map(): Map<Int, Int> { System.err.println("[main] testMap bean loaded...") return mapOf(10 to 11, 12 to 13) } }
test目录下的Kotlin代码
package com.example.demo // in test folder @SpringBootTest class DemoApplicationTests { @Autowired lateinit var map: Map<Int, Int> @Test fun contextLoads() { System.err.println("this is test") System.err.println("[test] map : $map") } } //@Configuration("test") open class Show { init { System.err.println("[test] this is Test#init") } @Bean("map") fun map(): Map<Int, Int> { System.err.println("[test] map bean loaded...") return mapOf(1 to 2, 3 to 4) } }
运行结果
执行DemoApplicationTests的contextLoads()方法时,预期会扫描main目录下带@Configuration注解的Show类,控制台打印[main]相关日志,但实际输出为:
[test] this is Show#init [test] map bean loaded... this is test [test] map : {1=2, 3=4}
问题
不理解为何测试包中未添加@Configuration注解的Show类会被扫描加载,请问是否有相关文档可以解释该现象?谢谢。
内容的提问来源于stack exchange,提问作者rura6502
相关产品推荐
相关产品推荐

