我正在使用 maven,并将我的配置文件作为“singleton-config.xml”包含在 src/main/resources 下。
当我创建上下文时
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:singleton-config.xml");
它工作正常,但是当我使用通配符时
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:singleton-*.xml");
它给出了异常
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'singleton' is defined.
谢谢
最佳答案
在类路径后使用通配符(“classpath*”)
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:singleton-*.xml");
说明
The Classpath*: portability classpath*: prefix
When constructing an XML-based application context, a location string may use the special classpath*: prefix:
ApplicationContext ctx =
new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");
This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(…) call), and then merged to form the final application context definition.
参见Wildcards in application context constructor resource paths了解更多信息。
关于java - Spring 通配符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37203087/