【问题标题】:Add slf4j dependency with Ant to Spring 4.0 application将 slf4j 依赖与 Ant 添加到 Spring 4.0 应用程序
【发布时间】:2013-12-27 18:03:00
【问题描述】:

我使用的是 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。我也加了。

请帮我解决问题。

【问题讨论】:

    标签: java spring spring-4


    【解决方案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>
    

    【讨论】:

    • 嗨 Evgeniy Dorofeev。谢谢回复。我没有使用基于 Maven 的配置。我正在使用基于 xml ant 的配置。你能帮我吗
    • 只需将上述所有依赖项,但 commons-logging 添加到您的类路径
    【解决方案2】:

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

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

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

    【讨论】:

    【解决方案3】:

    只需将jcl-over-slf4j.jar 文件添加到您的库中即可。详情请见slf4j docu

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 2012-12-30
      • 2012-11-01
      • 1970-01-01
      • 2011-12-02
      • 2020-12-27
      相关资源
      最近更新 更多