1. 程式人生 > >初識spring,一個spring的簡單例子出現的錯誤

初識spring,一個spring的簡單例子出現的錯誤

Person.java

package com.example.spring;

public class Person {     private String name;     private int age;     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public int getAge() {         return age;     }     public void setAge(int age) {         this.age = age;     } }

Testico.java

package com.example.spring;

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Testioc {     public static void main(String[] args) {          ApplicationContext context = new ClassPathXmlApplicationContext("My_Beans.xml");          Person p = (Person)context.getBean("person");          System.out.println(p);          p.setAge(16);          System.out.println(p.getAge());     }

}

My_Beans.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"     xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person" class="com.example.spring.Person">         <property name="name" value="zje"></property>         <property name="age" value="24"></property>     </bean> </beans>

程式碼執行後出現了這個錯誤:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [My_Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [My_Beans.xml] cannot be opened because it does not exist     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)     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:537)     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)     at com.example.spring.Testioc.main(Testioc.java:13) Caused by: java.io.FileNotFoundException: class path resource [My_Beans.xml] cannot be opened because it does not exist     at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)     ... 13 more 出現這錯誤是沒有指定完整路徑