You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBootApplication无法在Java类中导入的问题求助

解决无法导入SpringBootApplication注解的问题

核心原因

父模块仅继承了Spring Boot的parent依赖管理,但子模块需要显式引入包含@SpringBootApplication注解的starter依赖,才能在代码中使用该注解。

具体解决步骤

  1. 定位启动类所在子模块
    你的Application类大概率在book-api模块(通常启动类放在对外的api模块),打开该模块的pom.xml文件。

  2. 添加Spring Boot基础依赖
    在<dependencies>标签中加入以下依赖:

    <dependencies>
        <!-- 基础Spring Boot依赖,包含@SpringBootApplication注解 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- 若为Web项目,需额外引入web场景依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
  3. 验证子模块的父依赖继承
    确保子模块的pom.xml中正确继承了父模块,示例配置:

    <parent>
        <groupId>com.yizhen</groupId>
        <artifactId>yizhen-red-book-dev</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    
  4. 刷新依赖并修改启动类
    执行mvn clean install -U刷新依赖,之后在Application类中导入注解并添加启动方法:

    package com.imooc;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
  5. 处理IDE缓存问题
    如果IDE仍报错,尝试重启IDE并重新导入Maven项目。

内容的提问来源于stack exchange,提问作者tracking_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 14:37:22