SpringBoot-快速搭建

189 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动

环境要求

JDK1.8+

Maven3.3+/Gradle6+

Idea

maven&idea构建项目

  1. 使用IDEA新建项目,选择Spring Initializr;

Spring Initializr.png 2. 填写相关内容后,点击 Next 选择依赖的包再点击 Next,最后确定信息无误点击 Finish。

项目结构

|____src
| |____test
| | |____java
| | | |____com
| | | | |____miracle
| | | | | |____springboot
| | | | | | |____SpringbootApplicationTests.java
| |____main
| | |____resources
| | | |____static
| | | |____templates
| | | |____application.properties
| | |____java
| | | |____com
| | | | |____miracle
| | | | | |____springboot
| | | | | | |____SpringbootApplication.java
  • src/main/java 程序开发以及主程序入口

  • src/main/resources 配置文件

    static:保存所有的静态资源; js css images;

    templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf)

    application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;

  • src/test/java 测试程序

  • SpringbootApplicationSpring Boot项目入口类

依赖

spring-boot-starter-parent

这是Spring Boot的父级依赖,这样当前的项目就是Spring Boot项目了。 它用来提供相关的Maven默认依赖。使用它之后,常用的包依赖可以省去version标签。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

spring-boot-starter-web

核心模块,包括自动配置支持、日志和 YAML,如果引入了 spring-boot-starter-web web 模块可以去掉此配置,因为 spring-boot-starter-web 自动依赖了 spring-boot-starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter 相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器 。

打包

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这个插件,可以将应用打包成一个可执行的jar包。

创建控制器

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello() {
        return "hello";
    }
}

@RestController 的意思就是 Controller 里面的方法都以 json 格式输出。

启动&测试

运行SpringbootApplication中的main()方法启动容器。

@SpringBootApplication
public class SpringbootApplication {

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

}

容器启动.png

打开http://localhost:8080/hello测试一下。

更换启动banner

banner.png

自定义banner可以到以下网站: [字符画生成网站1](patorjk.com/software/ta… SpringBoot) 字符画生成网站2 字符画生成网站3

热部署

SpringBoot提供了热部署工具devtools,实现热代码部署是一件很简单的事情。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> 
        </dependency>

Idea需要修改以下两个地方:

  1. 勾选自动编译或手动编译

    File > Settings > Compiler-Build Project automatically

  2. 注册

    command + shift + A> Registry > 勾选compiler automake allow when app running

重启之后出现如下日志,说明热部署配置成功。

devtools.png

自定义配置

# 热部署开关,false即不启用热部署
spring.devtools.restart.enabled: true

# 指定热部署的目录
# spring.devtools.restart.additional-paths: src/main/java

# 指定目录不更新
spring.devtools.restart.exclude: test/**