【问题标题】:Dependencies issue "SpringBootServletInitializer cannot be resolved to a type"依赖问题“SpringBootServletInitializer 无法解析为类型”
【发布时间】:2017-11-13 02:41:52
【问题描述】:

我收到SpringBootServletInitializer cannot be resolved to a type 据我了解,这是一个依赖问题。

虽然我觉得编写 Java 代码很舒服,但这是我第一个使用 mavenspring-boot 的应用程序,自然我一无所知。

pom.xml:

<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>univers-web</artifactId>
    <packaging>war</packaging>

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

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

SpringBootApplication.java:

package com.thebyteguru.launcher;

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

public class SpringBootApplication extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootApplication.class);
    }

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

}

我可以看到相关的jar 文件存在于Maven Dependencies 文件夹中,而import'ing 需要class'es 我注意到package'es 在那里但它们是"empty"(意思是智能感知找到包,但不找到其中的类)。

我错过了什么?

【问题讨论】:

  • 您使用的是旧版本的 Spring Boot。您应该使用更新的版本开始。
  • @davidxxx 有什么特别的理由不使用旧版本吗?我理解为什么一个更讨厌的版本会更好,但我只是在学习使用这些工具,并且只是按照在网络上可以找到的提示和信息进行操作。
  • 因为 Spring boot 在 1.5 之前相当不稳定。例如,您缺少的类 (org.springframework.boot.context.web.SpringBootServletInitializer; ) 已被弃用很长时间。你想尝试做一些你不应该再使用的工作吗?
  • @davidxxx 我没有意识到我正在学习的东西是那么古老......谢谢。另外你介意给我一个正确的“entry point代码”的例子吗?
  • @davidxxx 顺便说一句,我将版本更改为 1.5.3 后,问题就解决了。所以谢谢。我仍然想知道启动应用程序的正确方法。

标签: java spring maven spring-boot


【解决方案1】:

我有同样的问题。我的解决方案是

而不是

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

使用

import org.springframework.boot.web.support.SpringBootServletInitializer;

也在 pom 中

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

【讨论】:

  • 现在 (2.0.1-RELEASE) 移至 org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  • 面临同样的问题...此外,如果您使用上下文,它也会在 SB2.0 中从 server_context-path 更改为 server.servlet.context-path
【解决方案2】:

不要做任何新的事情,只需在您的 Eclipse 中按 Ctrl+Shift+o 即可将正确导入添加到您的项目中

【讨论】:

  • 并且 应该给你import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;(而不是过时的 Spring Boot 1.x "web.support.SpringBootServletInitializer")。
【解决方案3】:

我遇到了同样的问题......但我正在使用带有 Spring Boot 2.1.1 的 Maven。

所以我需要指定正确的父级...并且更改导入/类名:

pom.xml:

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

MyApp.java:

...
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
// import org.springframework.boot.web.support.SpringBootServletInitializer;  // OBSOLETE
import org.springframework.boot.web.servlet.support.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);
    }
...

【讨论】:

    【解决方案4】:

    我正在学习相同的课程,我只是这样设置 pom:

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

    效果很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 2017-04-10
      • 2011-12-08
      相关资源
      最近更新 更多