0

我正在使用 spring 4.0,我从 hello world 示例开始。当我运行示例时,我得到了这个异常,

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:153)
    at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:100)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:60)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:82)
    at hello.service.main.Main.main(Main.java:14)
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)
    ... 5 more

但我读到,如果我们不使用公共日志记录并且我想使用 slf4j,那么我只想在类路径中添加 slf4j 日志记录 jar。我也添加了那个。

请帮助我解决问题。

4

3 回答 3

1

要使用 slf4j,您的 pom.xml 必须包含以下一组依赖项

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.0.RELEASE</version>
        <scope>runtime</scope>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.5.8</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.5.8</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.8</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <scope>runtime</scope>
    </dependency>
于 2013-12-24T09:32:04.737 回答
0

下载 commons-logging.jar 并将其添加到您的类路径中。

http://commons.apache.org/proper/commons-logging/download_logging.cgi

下载 zip 文件 commons-logging-1.1.3-bin.zip 并解压 commons-logging.jar,将其添加到类路径中,即可解决问题。

于 2013-12-24T09:08:30.137 回答
0

只需将jcl-over-slf4j.jar文件添加到您的库中。有关详细信息,请参阅slf4j 文档

于 2013-12-24T09:39:26.663 回答