照书上的例子做的,结果运行时总是出以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at onlyfun.caterpillar.SpringDemo.main(SpringDemo.java:12)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:104)
... 1 more
程序代码如下:
HelloBean.java
package onlyfun.caterpillar;
public class HelloBean {
private String helloWord;
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
} public String getHelloWord() {
return helloWord;
}
}
--------------------------------------
SpringDemo.java
package onlyfun.caterpillar; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory; public class SpringDemo {
public static void main(String[] args) {
Resource rs =
new ClassPathResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean hello =
(HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
-----------------------------------------------------
beans-config.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-2.0.xsd">
<bean id="helloBean"
class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>Hello!Justin!</value>
</property>
</bean>
</beans>
Exception in thread "main" java.lang.ExceptionInInitializerError
at onlyfun.caterpillar.SpringDemo.main(SpringDemo.java:12)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:104)
... 1 more
程序代码如下:
HelloBean.java
package onlyfun.caterpillar;
public class HelloBean {
private String helloWord;
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
} public String getHelloWord() {
return helloWord;
}
}
--------------------------------------
SpringDemo.java
package onlyfun.caterpillar; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory; public class SpringDemo {
public static void main(String[] args) {
Resource rs =
new ClassPathResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean hello =
(HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
-----------------------------------------------------
beans-config.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-2.0.xsd">
<bean id="helloBean"
class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>Hello!Justin!</value>
</property>
</bean>
</beans>
Exception in thread "main" java.lang.ExceptionInInitializerError
at onlyfun.caterpillar.SpringDemo.main(SpringDemo.java:12)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:104)
... 1 more
程序代码如下:
HelloBean.java
package onlyfun.caterpillar;
public class HelloBean {
private String helloWord;
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
} public String getHelloWord() {
return helloWord;
}
}
--------------------------------------
SpringDemo.java
package onlyfun.caterpillar; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory; public class SpringDemo {
public static void main(String[] args) {
Resource rs =
new ClassPathResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean hello =
(HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
-----------------------------------------------------
beans-config.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-2.0.xsd">
<bean id="helloBean"
class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>Hello!Justin!</value>
</property>
</bean>
</beans>
(HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
你的HelloWord没赋值,加一句hello.serHelloWord("dfdfd");
HelloBean hello =
(HelloBean)ac.getBean("helloBean");
楼主用这个测试下,如果不行再把问题贴出来看看