2010-12-28 9 views
2
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
       "application-context.xml")); 

私のアプリケーション-context.xmlには、私はこのエラーを取得し、それはロード取得する方法ロードアプリケーションのcontext.xml

...

INFO - XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [application-context.xml] 
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [application-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [application-context.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.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73) 
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61) 
    at com.mysticcoders.mysticpaste.services.CrudService.main(CrudService.java:9) 
Caused by: java.io.FileNotFoundException: class path resource [application-context.xml] cannot be opened because it does not exist 
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) 
    ... 4 more 

com.myname.mysubpackage.spring.application-context.xmlの内側に存在します。

答えて

3
new XmlBeanFactory(new ClassPathResource(
       "application-context.xml")); 

ファイルはデフォルトのパッケージに直接含まれている必要があります。

あなたはMavenを使用している場合は、クラスパスの理解に問題があるならば、最高の場所はresource DIR

+0

これを主なリソースに移動すると、dirは完全に機能しました。ありがとう。 –

0

にそれを置くために、代わりに、あなたはこのように、ファイル・システムから、あなたのコンテキストファイルを呼び出します(ただし、お勧めできませんが、それは)一時的にあなたの問題を解決:

File file = new File("/path/" + "Test.xml"); 
FileSystemResource fileResource = new FileSystemResource(file); 
XmlBeanFactory mFactory = new XmlBeanFactory(fileResource); 

しかし、最良の利用状況は、リソースディレクトリに設定ファイルを配置すると、あなたのクラスパスにこのディレクトリを追加することです。 Mavenを使って定義するのは本当に簡単です。

関連する問題