gpt4 book ai didi

java - spring - 从类路径加载 GenericXmlApplicationContext

转载 作者:行者123 更新时间:2023-12-01 23:25:46 34 4
gpt4 key购买 nike

我刚刚踏入 Spring 。

我不明白为什么当我编写一些简单的示例并尝试使用 ApplicationContents 加载它时,如下所示:

package com.appres.prospring3.ch5.factory;

public class MessageDigestExample {
public static void main(String[] args) {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("classpath:factory/factory.xml");
context.refresh();

MessageDigester digester = (MessageDigester) context.getBean("digester");
digester.digest("Hello World !!!!!!!!!");
}
}

就在这一行之后:

context.load("classpath:factory/factory.xml");

发生异常消息:

17:59:44,480 INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean >definitions from class path resource [factory/factory.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: >IOException parsing XML document from class path resource [factory/factory.xml]; nested >exception is java.io.FileNotFoundException: class path resource [factory/factory.xml] >cannot be opened because it does not exist

在我看来,一切都应该有效。我不明白这里出了什么问题。

这是我的项目结构:

class path into package

但是当我将 myFile.xml 移动到 resources 包时:

resources path

并将 context.load() 更改为 context.load("classpath:factory.xml");

一切正常,我可以看到正确的结果:

Using digest1
Using algorithm: SHA1
[B@5e9ed26e
Using digest2
Using algorithm: MD5
[B@d09644a

编辑:

当然,我尝试了最长的路径来加载此 .xml 文件,如下:

context.load("classpath:com/appress/prospring3/ch5/factory/factory.xml");

它会抛出一堆异常:

Exception in thread "main" 18:15:22,385  INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean definitions from class path resource [com/appress/prospring3/ch5/factory/factory.xml]
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/appress/prospring3/ch5/factory/factory.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/appress/prospring3/ch5/factory/factory.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.GenericXmlApplicationContext.load(GenericXmlApplicationContext.java:105)
at com.appres.prospring3.ch5.factory.MessageDigestExample.main(MessageDigestExample.java:8)
Caused by: java.io.FileNotFoundException: class path resource [com/appress/prospring3/ch5/factory/factory.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 7 more

- 为什么会发生这种情况?
- 是否存在某种方式从同一位置执行此操作。 main() 在哪里?

最佳答案

第一个版本有两个问题。

  1. xml 文件位于 src/main/java 下,而不是位于 src/main/resources 下。当 Maven 构建项目时,它期望仅在 src/main/java 下查找 Java 源文件,并忽略所有其他文件。资源文件必须放在src/main/resources下。这就是 Maven 约定。
  2. 代码从资源factory/factory.xml 加载文件。这意味着factory.xml 应该在包factory 中才能找到。但该文件位于 com.appres.prospring3.ch5.factory 包中。

关于java - spring - 从类路径加载 GenericXmlApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20032917/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com