Spring无法从ApplicationContext xml文件加载类

时间:2014-04-22 01:43:53

标签: java spring applicationcontext

我在尝试在下面创建ApplicationContext的新实例时遇到类未找到异常。有什么我想念的吗?

 package com.john;

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

public class Cook {

    /**
     * @param args
     */
    public static void main(String[] args) {


         ApplicationContext ctx = new ClassPathXmlApplicationContext("/resources/test.xml");
         Hamburger hamburger =(Hamburger)ctx.getBean("hamburger");
        hamburger.addMeat();
    }
}

汉堡课程:

 package com.john;

public class Hamburger  {

    void addMeat() {
        System.out.println("Meat is  added");

    }


}

我在src文件夹下的资源包中的test.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="hamburger" class="com.john.Hamburger"></bean>

</beans>

我得到的错误是:

Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 8 more

1 个答案:

答案 0 :(得分:1)

您需要下载Apache Commons Logging并将其添加到运行时类路径中。