【问题标题】:"The type org.springframework.web.WebApplicationInitializer cannot be resolved. It is indirectly referenced from required .class files"“无法解析 org.springframework.web.WebApplicationInitializer 类型。它是从所需的 .class 文件中间接引用的”
【发布时间】:2017-10-05 08:12:35
【问题描述】:

这是包含错误的文件..int 类它说错误“ org.springframework.web.WebApplicationInitializer 类型无法解析。它间接引用自 所需的 .class 文件 - SpringBootWebApplication 类型的层次结构不一致”

 package java.com;

        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.boot.builder.SpringApplicationBuilder;
        import org.springframework.boot.context.web.SpringBootServletInitializer;

        @SpringBootApplication
        public class SpringBootWebApplication extends SpringBootServletInitializer {

            @Override
            protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                return application.sources(SpringBootWebApplication.class);
            }

            public static void main(String[] args) throws Exception {
                SpringApplication.run(SpringBootWebApplication.class, args);
            }

        }

这是 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-web-jsp</artifactId>
    <packaging>war</packaging>
    <name>Spring Boot Web JSP Example</name>
    <description>Spring Boot Web JSP Example</description>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Web with Tomcat + Embed -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.6.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- Optional, for bootstrap -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 请分享完整的异常或堆栈跟踪。这可能会有所帮助
  • 这一行有多个标记 - org.springframework.web.WebApplicationInitializer 类型无法解析。它是从所需的 .class 文件中间接引用的 - SpringBootWebApplication 类型的层次结构不一致
  • 删除文件夹.m2\repository\org\springframework,重新安装spring依赖,我也遇到类似问题,重新安装后没有发现问题

标签: java spring-boot image-uploading


【解决方案1】:

这里的 Spring Boot 应用程序正在扩展 SpringBootServletInitializer。如果我查看 SpringBootServletInitializer 它实现了 WebApplicationInitializer。 添加以下具有适当版本的依赖项,将解析 SpringBootServletInitializer。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.8.RELEASE</version>
</dependency>

【讨论】:

    【解决方案2】:

    这意味着:“您使用的一个类需要另一个不在类路径上的类。”您应该确保将所需的 jar 添加到类路径中。

    如果你用的是eclipse,这是因为jdk版本不同,改成正确的,clean and build吧!

    【讨论】:

    • 嗨,@Rajith Pemabandu,如果可能,请检查一下,谢谢。 *.com/questions/42968587/…
    • 那么您能否建议我一种纠正此问题的方法或查找我需要添加哪个 jdk 版本的方法。这是在我将我的朋友项目部分添加到我的项目之后发生的..
    最近更新 更多