1.test类如下
[code="java"]public class SpringTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
SimpleMan dao = (SimpleMan) ctx.getBean("simpleMan");
System.out.println(dao.hello());
}
}
[/code]
2.beans.xml代码如下
[code="java"]<?xml version="1.0" encoding="UTF-8"?>
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">
context:annotation-config/
[/code]
3.test类和beans.xml配置文件都在包org.example下
4.spring2.5的架包从官网上下载的,都拷贝在了WEB-INF的lib目录下
5.两种情况
第一种:lib只引入spring.jar包
第二种:lib引入了dist/modules下的包,共14个
6.无论哪种情况都出现了如下错误:
[code="java"]Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:146)
at org.springframework.context.support.AbstractRefreshableApplicationContext.(AbstractRefreshableApplicationContext.java:84)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.(AbstractRefreshableConfigApplicationContext.java:59)
at org.springframework.context.support.AbstractXmlApplicationContext.(AbstractXmlApplicationContext.java:58)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:136)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at org.example.SpringTest.main(SpringTest.java:10)
[/code]
何解呢?谢谢大家了。。
[b]问题补充:[/b]
将commons-logging.jar包拷贝到lib目录下依然找不到beans.xml
现在有个方法能解决了:
1.将ClassPathXmlApplicationContext换成FileSystemXmlApplicationContext
2.将beans.xml拷贝到根目录下,即WebContent目录下
现在不报错了
什么原因,唉,我也不明白