package onlyfun.caterpillar;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SpringDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource rs = new ClassPathResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean hello = (HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloword());
}
}
package onlyfun.caterpillar;
public class HelloBean {
private String helloword;
public String getHelloword() {
return helloword;
}
public void setHelloword(String helloword) {
this.helloword = helloword;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<bean id="helloBean" class="onlyfun.caterpillar.Hellobean">
<property name="helloword">
<value>hello!</value>
</property>
</bean>
</beans>
我抱着非常古老的spring2.0的书去学习,为什么这都能报错误啊
log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [onlyfun.caterpillar.Hellobean] for bean with name 'helloBean' defined in class path resource [beans-config.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: onlyfun/caterpillar/Hellobean (wrong name: onlyfun/caterpillar/HelloBean)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1144)
------解决思路----------------------
配置文件时,要认真点:
<bean id="helloBean" class="onlyfun.caterpillar.Hellobean">
class配错了,认真看看。
log4j那些警告不影响使用,要配正确的话,需要在classPath中加入 log4j.properties文件,文件内容可以百度一下。
可能还需要初始化 log4j,这个忘记了是不是必须的。