Spring報錯:java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist


感謝:http://blog.chinaunix.net/uid-20681545-id-184633.html提供的解決方案,非常棒 !

問題說明:

新建一個Spring項目,新建一個Bean類:HelloWorld類,Main.java是主程序,xml是Spring配置文件。

項目結構如下:

 

打開文件夾,src目錄下的結構如下: 

 

打開bin文件夾,目錄如下

 

HelloWorld.java代碼:

 1 package com.tt.spring.beans;
 2 
 3 public class HelloWorld {
 4     
 5     private String name;
 6 
 7     public void setName(String name) {
 8         this.name = name;
 9     }
10     
11     public void hello(){
12         System.out.println("hello: "+name);
13     }
14 
15 }

Main.java

 1 package com.tt.spring.beans;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args){
 9         /*
10         //創建HelloWorld的一個對象
11         HelloWorld helloWorld = new HelloWorld();
12         //為name屬性賦值
13         helloWorld.setName("tt");
14         */
15         //調用hello方法
16         
17         //1.創建Spring 的 IOC 容器對象
18         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
19         
20         //2.從IOC容器中獲取Bean實例
21         HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
22         
23         //3.調用Hello方法
24         helloWorld.hello();
25     
26     }
27 }

applicationContext.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <!-- 配置bean -->
 7     <bean id="helloWorld" class="com.tt.spring.beans.HelloWorld">
 8          <property name="name" value="Spring"></property>
 9     </bean>
10 </beans>

運行Main.java程序,結果顯示報錯。報錯如下:

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tt.spring.beans.Main.main(Main.java:18)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.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:329)
... 13 more

 

 

問題分析:

個人的文件目錄樹大概這樣:
 
SpringDemo根目錄
   --src java源文件
   --WEB-INF
        --web.xml
        --applicationContext.xml
        --classes文件夾
           --com包
              --java編譯后的class文件
   --index.jsp

 

根據網上查找的解決辦法,我把applicationContext.xml從bin的com層的最里面復制出來放到classess文件夾即bin文件夾中(不能放到com中,應為xml中class路徑),該問題解決了。
現在目錄樹如下:

 

   --src java源文件
   --WEB-INF
         --web.xml
         --classes文件夾
             --applicationContext.xml
             --com包
                  --java編譯后的class文件
   --index.jsp
 
但是,當新建另一個Bean:Car類時,問題馬上來了!!!!!!!
 1  1 package com.tt.spring.beans;
 2  2 
 3  3 public class Car {
 4  4 
 5  5     private String brand;
 6  6     private String corp;
 7  7     private int price;
 8  8     private int maxSpeed;
 9  9     
10 10     
11 11     public Car() {
12 12         System.out.println("Car's Constructor");
13 13     }
14 14 
15 15     public Car(String brand, String corp, int price) {
16 16         super();
17 17         this.brand = brand;
18 18         this.corp = corp;
19 19         this.price = price;
20 20     }
21 21 
22 22     @Override
23 23     public String toString() {
24 24         return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
25 25     }
26 26     
27 27 }

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5                         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6                         
 7    <context:component-scan base-package="com.tt.spring.beans"></context:component-scan>
 8     <!--
 9                      配置bean 
10         class:bean的全類名,通過反射的方式在IOC容器中創建Bean實例,所以要求Bean中必須有無參的構造器
11         id:標識容器中的bean,id唯一
12     -->
13     <bean id="helloWorld" class="com.tt.spring.beans.HelloWorld">
14          <property name="name" value="Spring"></property>
15     </bean>   
16     
17     <!-- 通過構造方法來配置bean的屬性 -->
18     <bean id="car" class="com.tt.spring.beans.Car">
19          <constructor-arg value="Audi"></constructor-arg>
20          <constructor-arg value="chengdu"></constructor-arg>
21          <constructor-arg value="30000"></constructor-arg>
22     </bean>
23     
24 </beans>

Main.java

 

控制台打印信息如下:

 

 

看到沒?只能獲取一個bean實例,不能獲取兩個。

原因分析:

在項目里,applicationContext.xml是在src最里面一層的,不在src的那一層,所以一旦運行Main.java文件,相應的就會在bin的目錄下生成對應的applicationContext.xml文件。

但是我們又在bin文件夾里做過粘貼applicationContext.xml。

 

所以說applicationContext.xml的目錄壓根就是混亂的!!!既是處於根目錄,又處於com最里面的目錄。

要么在項目上將xml移到src那一層,那么bin底下的xml也對應着-------xml就處於根目錄。

要么在項目上仍然位於com的最里面,那么bin那里就不要將xml移出來----xml就處於最里面的目錄

至於xml文件究竟在哪個目錄,就該用相應的讀取方式。

 

ClassPathXmlApplicationContext和FileSystemXmlApplicationContext讀取xml

 
  ApplicationContext applicationContext = new FileSystemXmlApplicationContext("WEB-INF/classes/applicationContext.xml"); 

 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml") 
 
 現在這個項目是使用ClassPathXmlApplicationContext讀取xml文件的,就把配置文件appicationContext.xml放到src的那一層。

 

如果使用FileSystemXmlApplicationContext讀取,則需把配置文件放到整個項目的根目錄。
 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM