簡體   English   中英

春天-從類路徑加載GenericXmlApplicationContext

[英]spring - GenericXmlApplicationContext loading from classpath

我剛到春天。

我不明白為什么當我寫一些簡單的示例並嘗試按如下方式將其與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-從類路徑資源[factory / factory.xml]加載XML bean>定義從類路徑資源[factory / factory.xml]中解析XML文檔; 嵌套> exception是java.io.FileNotFoundException:類路徑資源[factory / factory.xml]>無法打開,因為它不存在

在我看來,所有人都應該工作。 而且我不知道這里出了什么問題。

這是我的項目結構:

包中的類路徑

但是,當我將myFile.xml移到resources包時:

資源路徑

並將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包中。

將完整的軟件包名稱添加到路徑后,第一個版本也將運行:

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

最好將配置文件保留在resources文件夾中,因為按照慣例,配置文件應始終放在該文件夾中。

classpath:前綴始終相對於您的類路徑的根。 因此

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

在類路徑的根目錄/factory/factory.xml中尋找factory.xml ,您顯然沒有。 您需要輸入完全合格的軟件包名稱才能找到它所在的位置

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

src/main/resources文件夾是Maven約定。 Maven將獲取該文件夾中的所有文件,並將它們添加到類路徑的根目錄(或相對於它們嵌套的文件夾/包的根目錄。)因此,在您的第二個示例中, factory.xml找到了到達該類路徑的根目錄的方式,您可以對其進行訪問與

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM