创建一个简单入门SpringBoot3项目

174 阅读3分钟

创建空项目

  • File—New—Project

  • Empty Project

    • Name:项目名
    • Location:项目存放位置

image.png

image.png

image.png

创建模块

模块名,有意义即可,如:coder-starter(启动模块)

  • 右键项目—New—Module...

image.png

image.png

image.png

pom.xml文件

  • 添加Spring Boot 父项目依赖
  • 添加web开发的场景启动器依赖
  • 点击加载依赖包图标进行加载
<!-- Spring Boot 父项目依赖 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.5.3</version>
</parent>

<dependencies>
    <!-- Web 开发场景启动器依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

image.png

主程序

  • com/zibocoder/starter 目录下创建启动类 MainApplcation
  • 类上添加注解 @SpringBootApplication
  • 启动 main 方法
package com.zibocoder.starter;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
/**
 * 启动类
 * @author zibocoder
 * @date 2025/6/27 10:18:02
 */
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
        System.out.println("启动成功");
    }
}

image.png

默认使用 Tomcat 中间件运行应用程序,默认端口 8080

编写业务

  • com/zibocoder/starter/controller 目录下创建测试类 TestController
package com.zibocoder.starter.controller;
​
​
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
/**
 * 测试控制器
 * @author zibocoder
 * @date 2025/6/27 10:33:14
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello SpringBoot3";
    }
}

测试

  • 重启启动类,访问 http://localhost:8080/test/hello
  • 页面显示 Hello SpringBoot3,说明成功

编译打包

  • pom.xml 中添加 maven 编译和打包插件代码

  • 点击加载依赖包图标进行加载

  • 右边点击 Maven 工具按钮,展开操作页面

  • 模块名—Lifecycle

    • clean:删除上次构建生成的文件(通常是 target/ 目录)

    • compile:编译项目的源代码( src/main/java 下的 .java 文件),并将编译后的 .class 文件输出到 target/classes/ 目录。

    • package:在 compile 的基础上,将项目打包成可发布的格式,如 JARWAR 等,具体取决于项目的 pom.xml 配置。

      • 编译源代码(触发 compile
      • 运行单元测试(默认情况下)
      • 打包构建产物(如生成 JAR/WAR 文件)
...
    <build>
        <plugins>
            <!-- Maven 编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <!-- 设置编译时的JDK版本 -->
                    <source>21</source>
                    <target>21</target>
                    <!-- 设置编码为UTF-8 -->
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
​
            <!-- Maven 打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            
            <!-- Maven 打包时跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>            

image.png

运行jar文件

  • 进入 jar 文件所在目录
  • 命令:java -jar xxx.jar

image.png

yaml配置文件

  • src/main/resources 目录下创建文件 application.yml
  • 更改默认配置,如端口号改为 1024
server:
  port: 1024

image.png

更换Web服务器

使用 Undertow 来替代 Tomcat

  • Undertow 是一个采用 Java 开发的灵活的高性能 Web 服务器
  • 高性能非阻塞架构:采用事件驱动方式处理请求,适合高并发场景。
  • 轻量级:比 Tomcat 内存占用更低。
  • 支持现代协议:HTTP/2、WebSockets、Servlet 4.0、HTTPS ALPN 等。
  • 可嵌入性强:易于与 Spring Boot 等框架集成。
  • pom.xmlspring-boot-starter-web 中排除 tomcat
  • 添加 undertow 依赖 spring-boot-starter-undertow
  • 点击加载依赖包图标进行加载
  • 启动服务
...
    <!-- Web 开发场景启动器依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <!-- 排除 Tomcat -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
​
    <!-- 添加 Undertow -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
...

image.png

如有帮到你可以点赞,收藏或鼓励一下,谢谢(^_^),有什么疑问可以评论交流互相学习

收款码3.png