1 SpringBoot HelloWorld
功能:浏览器发送 sayHello 请求,服务器接受请求并处理,响应 Hello。
1.1 创建一个maven工程
<groupId>com.seagetech</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>1.0.0</version>
1.2 下载官方参考文档
在官网找到相应的下载地址(官网地址:https://spring.io/projects)或者直接使用如下地址下载:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/pdf/spring-boot-reference.pdf。文档名称 “spring-boot-reference.pdf”,版本2.10.RELEASE。
1.3 使用官方参考文档
Part II. Getting Started > 11. Developing Your First Spring Boot Application。
1.3.1 创建POM
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
1.3.2 添加 ClassPath 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
> </dependencies>
1.3.3 编写代码
创建一个主程序类,类名随便取。因为我们是做一个 HelloWorld 的项目,所有我们创建一个 HelloWorldApplication 类:
@Controller
@EnableAutoConfiguration
public class HelloWorldApplication {
@RequestMapping(value = "/sayHello")
@ResponseBody
public String sayHello(String name){
return "Hello," + name;
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
1.3.4 运行例子
启动 main 方法后,查看 SpringBoot 启动日志:
可以看到Tomcat的启动日志:
Tomcat started on port(s): 8080 (http) with context path ”
即,Tomcat 默认启动端口为 8080,项目根路径为 “”,所以在浏览器或者 Postman 中输入地址:http://localhost:8080/sayHello?name=wangzb,浏览器或Postman响应:
Hello,wangzb
1.3.5 创建一个可执行的 jar 文件
要创建一个可执行 jar,我们需要将 spring-boot-maven-plugin 添加到我们的 pom.xml 中。在dependencies部分下面插入以下几行:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
接下来就可以打包,在 Idea 中点击 package,执行打包命令,之后我们在项目的 target 包下就可以看到刚打包好的 springboot-helloworld-1.0.0.jar 包。
2 HelloWorld探究
2.1 使用IntelliJ IDEA快速创建SpringBoot项目
- 新建项目
- 左边菜单选择 Spring Initializr,右边菜单选择 JDK 版本,然后点击Next
- 填写 Maven 坐标 GroupId/ArtifactId、打包形式(本例使用Jar的方式,war包形式请看后续文章)、项目名称/版本等基本信息,然后点击Next
- 选择 SpringBoot 版本,以及需要依赖的包,这里我们选择最新版2.1.0,导入Web模块依赖包,点击Next
- 修改项目名称以及项目存放路径,默认即可,点击 Finish
2.2 SpringBoot项目结构分析
通过 2.1 的讲解,我们熟悉了如何快速创建 SpringBoot 项目。接下来分析 SpringBoot 项目结构,上节中创建好的 SpringBoot 项目结构如下:
2.2.1 pom.xml文件
2.2.1.1 父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
按住Ctrl,点击 spring-boot-starter-parent 进入 spring-boot-starter-parent 项目,它的父项目是:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
如上,可以看到 spring-boot-dependencies。它是真正管理 SpringBoot 项目所有依赖版本,是 Spring Boot 的版本仲裁中心,以后我们导入依赖默认是不需要写版本(没有在 dependencies 里面管理的依赖自然需要声明版本号)。
2.2.1.2 启动器(Starter)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter:spring-boot 场景启动器。帮我们导入了 Web 模块正常运行所依赖的组件。Spring Boot 将所有的功能场景都抽取出来,做成许多启动器(starter),只需要在项目里面引入这些启动器,相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器。
- 以下列举 SpringBoot 官方提供的 starter pom
更多请参考 “spring-boot-reference.pdf” 2.1.0.RELEASE,13.5节Starters。
- 除了官方的 starter pom 外,还有第三方为 SpringBoot 所写的 starter pom
如下表所示:
2.2.2 resources文件目录结构
static:保存所有的静态资源,如 js、css、images 等。
templates:保存所有的模板文件,SpringBoot 默认 jar 使用嵌入式的 Tomcat,默认不支持 JSP 页面,可以使用模板引擎(Thymeleaf、Freemarker )。
application.properties:SpringBoot 应用配置文件,可以修改一些默认的配置,如修改 Tomcat 端口,项目根路径等。
2.2.3 主程序类
打开2.1节创建的 springboot-demo 项目,找到 SpringBootDemoApplication 类:
package com.seagetech.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
基本和1.3.3节创建的主程序类一样,main 方法加注解的形式,不同的是,Idea 创建的主程序类使用的是 @SpringBootApplication 注解,它是 SpringBoot 的核心注解,也是一个组合注解,打开这个注解,源码如下:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication 注解的核心功能其实是:@SpringBootConfiguration、@EnableAutoConfigration、@ComponentScan 三个注解提供。
网友评论