将Ant的slf4j依赖项添加到Spring 4.0应用程序中

时间:2013-12-24 08:57:15

标签: java spring spring-4

我正在使用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

但是我读到如果我们不使用commons日志记录并且我想使用slf4j那么我只想在类路径中添加slf4j logging jar。我也补充说。

请帮我解决问题。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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,将其添加到classpath,将解决问题。

答案 2 :(得分:0)

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