java - Spring 通配符不起作用

标签 java spring maven

我正在使用 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/

相关文章:

Java Applet 设置颜色 Action 监听器

Java .class 文件与 jar 中的 .java 文件

java - spring 错误 :org. springframework.web.context.ContextLoader - 上下文初始化失败

spring - 在 SPRING BOOT 中配置多个数据库

java - Pax-exam Karaf 安全 Maven 存储库身份验证

java - GridBagLayout 元素不移动

java - 客户方法的 UnsatisfiedDependencyException

java - 文件上传zuul代理请求大小问题

java - Maven Ant Task 尝试以 zip 格式打开 pom 但失败

java - 如何在 maven 中使用单个 groupId 和多个 artifactId?