我昨天对其进行了调查,发现Spring
@TestPropertySource仅在以下位置寻找此注释:
- 源测试类
- 接口是否由测试类实现
- 此测试类别的超级赛
- 继承的注释
这
AbstractTestContextBootstrapper.class是负责该代码的部分代码:
MergedTestPropertySources mergedTestPropertySources = TestPropertySourceUtils.buildMergedTestPropertySources(testClass);MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass, StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes), ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList), ActiveProfilesUtils.resolveActiveProfiles(testClass), mergedTestPropertySources.getLocations(), mergedTestPropertySources.getProperties(), contextCustomizers, contextLoader, cacheAwareContextLoaderDelegate, parentConfig);
该方法
TestPropertySourceUtils.buildMergedTestPropertySources(testClass)完全负责从此批注中查找和提取位置。如您所见,Spring仅在测试类上调用它。
所以,如果你想这个外部化注释,你需要创建一个超类,并把这个注释,并
@import在其上,或与此注释创建界面,或者创建自己的注解,将结合两个注解
@import,并
@TestPropertySource把它放在你的测试类。



