当前位置: 首页 > 知识库问答 >
问题:

spring中从xml文件中提取bean的错误

史逸春
2023-03-14

我是新来的Spring。我创建了config.xml,其中创建了bean。当我试图使用getbean时,我得到了以下错误:

IOException从类路径资源[config.XML]解析XML文档;嵌套异常是java.io.FileNotFoundException:无法打开类路径资源[config.xml],因为它不存在。

我之前用过这句话。

ApplicationContext context= new ClassPathXmlApplicationContext(" config.xml");

Student Student1=(Student) context.getBean("s1");
ApplicationContext context= new ClassPathXmlApplicationContext("classpath*: config.xml");

我的config.xml位于src/main/java/config.xml

config.xml的内容:

<?xml version="1.0" encoding="UTF-8" ?>


<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="s1" class="org.example.Student"> <!--org.example is groupId-->
    <property name="studentId">
        <value>22344</value>
    </property>
    <property name="studentName">
        <value>AP</value>
    </property>
    <property name="studentAddress">
        <value>L</value>
    </property>
</bean>

共有1个答案

陈兴朝
2023-03-14

必须将xml文件config.xml放入src/main/resources(默认情况下,maven将Java源文件放入src/main/Java,将资源文件放入src/main/resources)

在创建classpathXmlApplicationContext对象时,不需要classpath关键字,只需使用classpathXmlApplicationContext(“config.xml”),如下所示:

ApplicationContext context= new ClassPathXmlApplicationContext("config.xml");
Student student1=(Student) context.getBean("s1");
System.out.println(student1.getStudentId());
 类似资料:
  • 问题内容: 这是示例xml文档: 我想提取文本而不指定元素,我该怎么做,因为我有10个这样的文档。我想要这样做是因为我的问题是用户正在输入一个我不知道的单词,必须在其各自文本部分的所有10个xml文档中进行搜索。为此,我应该在不知道元素的情况下知道文本的位置。所有这些文档都不同的另一件事。 请帮忙!! 问题答案: 您可以简单地去除所有标签: 但是,如果您只想在Linux中搜索文件中的某些文本,则可

  • Q非常业余的程序员在这里,寻求你的帮助。 我必须经常编辑这样的xml文件 使用一个相当复杂的正则表达式搜索和替换过程,我只能提取标记属性的值。(这就是我所关心的)。 但是这很耗时,而且在Python中必须有非常简单的方法来查找属性标记="SOME_TEXT"部分并将所有值放入一个数组中,然后打印出该数组(到文件中)。但是我无法弄清楚:( 我正在寻找一种不包括导入任何类型的XML库的方法,因为我想让

  • 我有两个应用程序: > 应用程序父应用程序:Spring Boot应用程序。将应用程序子级用作库。 是否可以加载XML中定义的所有bean,并将它们放入上下文中?

  • 在我的Spring Boot项目中,我定义了4个配置文件ide、dev、test和Prod。当我使用IntelliJ和profile ide运行项目时,一切都运行得很好,并且可以从存储库中的ide配置文件中检索属性。但在dev server中部署期间,当我使用参数选择开发配置文件时,正在选择开发配置文件,但正在检索ide配置文件属性 CustomVaultConfigurer.java 错误日志:

  • 我有一个定义为原型的bean作用域,它引用另一个作用域为singleton的bean。从spring文档中,我知道 单例 这将bean定义的范围限定为每个Spring IoC容器的一个实例(默认)。 原型 这将单个bean定义的范围限定为具有任意数量的对象实例。 下面是我的代码: 这是否意味着我在每次需要一个新的员工bean实例时强制Spring生成一个新的员工bean实例,并且部门bean在每个

  • 我需要从大文件中提取文本(最大限制50MB)文件可能是doc、ppt、xls、txt或pdf格式。到目前为止,我使用了ApachePOI'http://poi.apache.org/' 用于Microsoft Office文档和PDFBox从PDF中提取文本。然而,随着文件变大,提取过程变得缓慢,特别是以下文件。到目前为止我取得的成果: 1. PPTX-45MB-3分钟apx 2.PDF-62MB