
Spring项目初始化应使用Spring Initializr并正确配置spring-boot-starter-parent和spring-boot-starter-web,确保parent声明、依赖引入、启动类位置(根包)及配置文件路径/编码无误。
Spring 项目初始化不是“配环境”,而是选对起点——用 spring-boot-starter-parent 和 spring-boot-starter-web 就能跳过 XML 配置、Servlet 容器部署、依赖版本冲突等老问题。
手动建 Maven 工程再加一堆 dependency 容易漏掉 spring-boot-maven-plugin 或写错 parent,直接访问 https://www./link/5ecc613150de01b7e6824594426f24f4:
com.example)、Artifact(如 demo)Spring Web(替代旧版的 spring-webmvc)mvn clean compile 能过就说明依赖和插件已就位很多人复制旧项目配置,把 spring-boot-starter-parent 写成普通 parent 或漏掉 version,导致 @RestController 找不到或 SpringApplication.run() 启动失败。
org.springframework.boot spring-boot-starter-parent3.2.5 org.springframework.boot spring-boot-starter-web
注意:spring-boot-starter-parent 不是可选的——它统一管理依赖版本、默认插件配置(比如 spring-boot-maven-plugin),删掉它就得自己写 和 ,得不偿失。
@SpringBootApplication 默认只扫描该类所在包及其子包。如果启动类放在 com.example.config,而 @RestController 在 com.example.controller(同级),就不会被加载。
Application.java,放在 com.example(即 Group ID 对应的最外层包)config、util 这类子包里@SpringBootApplication(scanBasePackages = "com.example")
application.yml 位置和常见坑配置文件必须放在 src/main/resources/ 下,名字必须是 application.properties 或 application.yml(不能叫 app.properties 或大小写错误)。
常见低级错误:
server.port=8080 写成 server.port = 8080(YAML 要求冒号后必须空格)spring.profiles.active=dev 写在 application-dev.properties 里(它不会自动激活自己,得写在主配置里)# 但文件编码不是 UTF-8(IDEA 默认是 UTF-8,Eclipse 可能是 GBK,导致乱码后配置失效)启动时看控制台第一行:如果看到 Started Application in X seconds,说明配置加载成功;如果卡在 Tomcat started on port(s): 8080 之后没日志,大概率是 @RestController 没被扫到或端口被占。