0

我有这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:164)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:89)
    at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:58)
    at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:61)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.tutorialspoint.MainApp.main(MainApp.java:10)

我已经添加了这些库

commons-collections-3.1
spring-aop-4.0.0.M1
spring-aspects-4.0.0.M1
spring-beans-4.0.0.M1
spring-context-4.0.0.M1
spring-context-support-4.0.0.M1
spring-core-4.0.0.M1
antlr-2.7.2

我的代码:

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

public class MainApp {
public static void main(String[] args) {
    System.out.println("hola");
    ApplicationContext context =
            new ClassPathXmlApplicationContext("Beans.xml");
    HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
    obj.getMessage();
}
 }


public class HelloWorld {

private String message;
public void setMessage(String message){
    this.message = message;
}
public void getMessage(){
    System.out.println("Your Message : " + message);
}
 }

配置

<?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-
 3.0.xsd"
 >
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
    <property name="message" value="Hello World!" />
</bean>
</beans>

提前致谢

4

3 回答 3

1

spring 框架可能在内部使用公共日志记录。因此,将 commons-logging.jar 添加到您的 lib 文件夹中应该可以解决问题。

Jar 可以从这里下载。

于 2013-06-12T08:41:48.437 回答
0

该代码需要一个日志记录实现。我相信 Spring 更喜欢SLF4J将以下内容添加到您的 pom 或类路径中:

<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
   <version>1.7.2</version>
</dependency>

请参阅有关登录 Spring的文档。

于 2013-06-12T08:45:06.613 回答
0

jarfinder.com这样的工具对此很有用。这个查询给了我

17 classes match your query :-

org.apache.commons.logging.LogFactory
....

并选择那个类给了我候选罐子

abstract class org.apache.commons.logging.LogFactory [6781 bytes, jvm >= 1.2 ]

    commons-logging.jar
    appserv-rt.jar
于 2013-06-12T08:41:33.120 回答