2014-03-30 51 views
0

我有我的Spring应用程序的应用程序上下文正确加载和工作。但是,当我尝试在我的测试中加载它时,加载的应用程序上下文不包含任何在配置文件中声明的bean。错误的弹簧应用上下文加载测试

@Test 
public void testInsertMapitPoint() { 
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {  "classpath:*/application-ds-context.xml","classpath:*/application-dao-context.xml" }); 
    System.out.println("Bean Names:"); 
    for (String beanName : context.getBeanDefinitionNames()) { 
     System.out.println(beanName); 
    } 
} 

显示的测试在上下文中不显示任何bean。任何帮助?

回答

0

有了这样

classpath:*/application-ds-context.xml 
     //^this guy 

春天的图案将加载该模式匹配的任何文件。 任何这意味着它可能找不到/加载任何。在这个例子中,它会查找任何第一级包中的名为application-ds-context.xml的文件。

例如,它会找到

/classpath-root 
    /first 
     application-ds-context.xml 

,但它不会找到

/classpath-root 
    application-ds-context.xml 

/classpath-root 
    /first 
     /second 
      application-ds-context.xml 

如果这是你所看到的行为,你的classpath不包含这样的文件。查看你如何构建测试应用程序。

+0

啊哈。我认为他的意思是'classpath *:/ application-ds-context.xml'。 – chrylis

+0

@chrylis看看'PathMatchingResourcePatternResolver'。在这种情况下,使用'*'作为你提出的和OP有什么本质上是相同的。 –

+0

@chrylis对不起,没有。不完全是。我想说的是他们都是通配符。但是,是的,'classpath *:/ application-ds-context.xml'会在根目录下查找它,而不是一个包级别。 –

相关问题