使用 Spring Initializr 初始化 Spring Boot 项目
Spring Initializr 从本质上说就是一个Web应用程序,它能为你构建Spring Boot项目结构。
虽然不能生成应用程序代码,但它能为你提供一个基本的项目结构,以及一个用于构件代码的Maven或者Gradle构建说明文件。
Spring Initializr 的几种用法
- 通过Web界面使用
- 通过Spring Tool Suite使用
- 通过IntelliJ IDEA使用
- 通过Spring Boot CLI使用
使用Web方式
要使用Spring Initializr,最直接的办法就是使用浏览器打开 https://start.spring.io,应该能看到一个类似于下图的一个表单。

配置
其中的内容,是需要我们选择的配置项,根据个人需求选择。
Project: 选择是使用Maven还是Gradle来创建项目。默认Maven,本次选择Maven。
Language: 选择使用的开发语言。默认Java,本次选择Java。
Spring Boot: 选择Spring Boot版本,默认最新版本(非里程碑和快照版本),也可以自由选择其他版本,本次选择最新的2.7.1.
Project Metadata: 指定项目的一些基本信息。最起码得提供项目的Group和Artfact 。点击Options展开,也可以配置一些额外的信息:
项目名称(name):本次为springboot_demo2.
项目描述(Description):本次默认
包名(Package Name):本次为com.tumbler.demo1
打包方式(Packaging):本次为jar方式
Java JDK版本号(Java):本次为1.8
当然这些额外信息都是后期可以修改的,Spring Boot的一大优势就是内嵌了Servlet容器,打成jar包后直接可以运行,所以建议打成jar包,当然开发者可以根据自己的需求打成war包。
Dependencies: 选择需要的依赖,输入关键字就有相应提示,我们选择需要的依赖即可,它会在创建项目时自动在生成的pom.xml(Maven)或者build.gradle(Gradle)引入依赖,当然也可以后期配置。本次选择web依赖即可。


导入IDEA
填好表单,选好依赖后,可以点击下方Generate the project 按钮或者使用快捷键Ctrl+Enter,Spring Initializr就会生成一个项目,浏览器将会以zip文件的形式(文件名取决于Artifact字段的内容)把这个项目下载下来。
解压后通过IDEA或者Eclipse将项目导入,导入后我们可以看到项目的基本目录结构如下

项目里基本没有代码,除了几个空目录外,还包含了如下几样东西:
里面定义了Spring Boot版本,groupId、artifactId、项目描述、jdk版本以及web起步依赖- <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.7.1</version>
- <relativePath/>
- </parent>
- <groupId>cn.he</groupId>
- <artifactId>springboot_demo2</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springboot_demo2</name>
- <description>Demo project for Spring Boot</description>
- <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-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
复制代码
- SpringbootDemo2Application.java 一个带有main方法的类,用于引导启动应用程序。
- package cn.he.springboot_demo2;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class SpringbootDemo2Application {
- public static void main(String[] args) {
- SpringApplication.run(SpringbootDemo2Application.class, args);
- }
- }
复制代码
- SpringbootDemo2ApplicationTests.java 一个空的JUnit测试类,它加载了一个使用Spring Boot自动配置功能的Spring应用程序上下文。
- package cn.he.springboot_demo2;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
- @SpringBootTest
- class SpringbootDemo2ApplicationTests {
- @Test
- void contextLoads() {
- }
- }
复制代码
- application.properties 一个空的properties配置文件,可以根据需求添加配置属性。
还有几个空目录:static目录存放的是web应用程序的静态内容(JavaScript、样式表、图片等等),templates目录用于存放呈现模型数据的模板。
这样通过Spring Initializr 的Web方式已经成功创建一个Spring Boot的项目,我们写一个Controller进行测试,controller包一定要在启动类所在包的子包。- package cn.he.springboot_demo2.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * @description: Spring Boot 测试 Controller
- * @author: 小小赫下士
- * @createDate: 2022/6/24
- */
- @RestController
- public class HelloController {
- @GetMapping("hello.action")
- public String hello(){
- return "Hello Spring Boot";
- }
- }
复制代码 启动项目
控制台可以看到使用内置容器Tomcat,端口号为8080。

启动成功,在浏览器输入:http://localhost:8080/hello.action
可以看到成功返回了Hello Spring Boot:

IDEA 上使用Spring Initializr
IDEA上是自带 Spring Initializr 集成的,只需要在新建项目是选择 Spring Initializr。

其他配置方式也是一样的

也可以手动选择依赖

新建好的效果和在Spring Initializr 网站生成的是一样的,这样操作更省事。
遇到的一些问题
1、启动时报错:无效的源发行版

需要在File - Project Structure - Module 里修改 Language level 为 8 :

2、启动时报错:无效目标发行版

需要在 File - Settings - Build,Exec..... - Compiler - Java Compiler 中把Target bytecode version 改为8

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |