在 Spring 'mvc-basic' 教程上实例化 SLF4J LoggerFactory 失败

问题描述 投票:0回答:2

我正在 Eclipse 3.7 上尝试 Spring 3.x 教程,安装了 m2e 和 Maven Integration for WTP 插件。

我将上面的教程转换为 Maven 项目,但是当我部署 Web 应用程序时,出现以下异常:

Failed to instantiate SLF4J LoggerFactory
Reported exception:
  java.lang.NoClassDefFoundError: org/apache/log4j/Level

我的 pom.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.samples</groupId>
    <artifactId>mvc-basic</artifactId>
    <name>mvc-basic</name>
    <packaging>war</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <properties>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
        <org.slf4j.version>1.6.1</org.slf4j.version>
    </properties>
    <dependencies>

        <!--  Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>runtime</scope>
        </dependency>

        <!--  JSR 303 with Hibernate Validator -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.1.0.Final</version>
        </dependency>

        <!--  Joda Time -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>1.6.2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time-jsptags</artifactId>
            <version>1.0.2</version>
            <scope>runtime</scope>
        </dependency>       

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <repositories>
        <!-- For Hibernate Validator -->
        <repository>
            <id>org.jboss.repository.release</id>
            <name>JBoss Maven Release Repository</name>         
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
            <snapshots><enabled>false</enabled></snapshots>         
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>mvc-basic</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我还检查了“项目属性”->“Java 构建路径”->“库”,但我发现的唯一 log4j jar 是“Maven 依赖项”下的那个

好像我在搞乱罐子,但我自己找不到解决方案。

spring maven log4j slf4j
2个回答
0
投票

尝试在eclipse中清理项目和服务器。这看起来是一个令人耳目一新的问题。

如果您在 Eclipse 中使用 Maven (M2E),那么这些库仅位于“Maven 依赖项部分”是正确的。

您可以从 Spring Source Tool Suite 模板项目(基于 Maven 的)开始,然后将教程中的代码复制到该项目中。

如果您使用 Tomcat,则 Eclipse 必须将库复制到

workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp<VERSION>\wtpwebapps\<PROJECT>\WEB-INF\lib
。 如果该库不存在,即使您引用了所有内容,Eclipse 也不会复制它。有时,您可以做的最后一件事是从服务器中删除项目(在“添加和删除...”对话框中)按“完成”,然后再次添加项目。


0
投票

看起来其他一些库依赖于 SLF4J。创建WAR文件,检查war中是否打包了SLF4J.jar。如果没有,请检查您的 Appservers 公共 lib 目录。万一它加载它。 检查 pom.xml 的依赖关系图。

如果您想添加 SLF4J log4j API,下面是依赖项。

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.4</version>
</dependency>
© www.soinside.com 2019 - 2024. All rights reserved.