我有一个非常简单的类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:/application-context-this-does-not-exist.xml"})
public class HTMLSourceExtractorImplTest {
@Autowired
ApplicationContext context;
@Test
public void test(){
String [] beans = context.getBeanDefinitionNames();
for(String bean : beans){
System.out.println(bean);
}
System.out.println("Testing");
}
}
在类路径中指定的上下文文件不存在。我可以随便起任何名字,代码不会出错。我的意思是测试运行得很好,就像该文件真的存在一样。
如果我进行一个小改变,从:classpath* 到 classpath,那么它会崩溃,并说这个文件不存在,这是我在第一种情况下也期望的行为。
Spring 版本 3.2.3.RELEASE。
有人能解释这种奇怪的行为吗?
编辑
建议从日志中获取以下信息:
20:47:26,923 INFO [GenericApplicationContext] Refreshing org.springframework.context.support.GenericApplicationContext@3df6c65c: startup date [Fri Jun 07 20:47:26 PDT 2013]; root of context hierarchy
我甚至尝试从应用程序上下文中输出所有的bean:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.ConfigurationClassProcessor.importAwareProcessor
我认为,在使用通配符的情况下,Spring会创建一个默认的空应用程序上下文。
classpath*:
是一个通配符前缀,作为搜索过滤器,而不是确定的位置。 - Pavel HoralSpringJUnit4ClassRunner
总是创建一个基本的应用程序上下文,然后将其与基于@ContextConfiguration
注释参数加载的所有配置合并。您提供的参数导致未加载任何配置,因此得到了空的上下文。 - Pavel Horal