Spring Boot入门

章节概要:

 随着互联网的兴起,Spring势如破竹,占据着Java领域轻量级开发的王者地位。随着Java语言的发展以及市场开发的需求,Spring推陈出新,推出了全新的Spring Boot框架。Spring Boot是Spring家族的一个子项目,其设计初衷是为了简化Spring配置,从而可以轻松构建独立运行的程序,并极大提高开发效率。

本章将从Spring Boot开发入门入手,带领大家正式进入SpringBoot框架的学习,并对Spring Boot的相关原理进行深入分析。

认识Spring Boot:

Spring Boot是基于Spring框架开发的全新框架,其设计目的是简化新Spring应用的初始化搭建和开发过程。 Spring Boot整合了许多框架和第三方库配置,几乎可以达到“开箱即用”

Spring Boot 优点:
  • 可快速构建独立的Spring应用    

  • 直接嵌入Tomcat、Jetty和Undertow服务器(无需部署WAR文件)

  • 提供依赖启动器简化构建配置 极大程度的自动化配置Spring和第三方库

  • 提供生产就绪功能

  • 极少的代码生成和XML配置

保证安装好的软件如下:
  • JDK 1.8.0_201(及以上版本)
  • Apache Maven 3.6.0
  • IntelliJ IDEA Ultimate旗舰版

IDEA欢迎页

初始化Maven设置

(1)单击【Configure】→【Project Defaults】→【Settings】进入设置Maven界面

(2)初始化Maven设置

(1)【Configure】→【Project Defaults】→【Project Structure】进入JDK设置页

(2)在界面左侧选择【Project Settings】→【Project】选项

使用Maven方式构建Spring Boot项目

  • 创建Maven项目
  • 在pom.xml中添加Spring Boot相关依赖
  • 编写主程序启动类
  • 创建一个用于Web访问的Controller
  • 运行项目

① 创建Maven项目

② 在pom.xml中添加Spring Boot相关依赖

  1. <!-- 引入Spring Boot依赖 --> <parent>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-parent</artifactId>     <version>2.1.3.RELEASE</version> </parent> <dependencies>     <!-- 引入Web场景依赖启动器 -->     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>     </dependency> </dependencies>

③ 编写主程序启动类

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

④ 创建一个用于Web访问的Controller

@RestController   public class HelloController { @GetMapping("/hello")    public String hello(){         return "hello Spring Boot";     } }

⑤ 运行项目

启动项目,在浏览器上访问 http://localhost:8080/hello

使用Spring Initializr方式构建Spring Boot项目

搭建步骤:

  1. 创建Spring Boot项目
  2. 创建一个用于Web访问的Controller
  3. 运行项目

① 创建Spring Boot项目

① 创建Spring Boot项目

创建好的项目结构如下图所示:

② 创建一个用于Web访问的Controller

@RestController   public class HelloController { @GetMapping("/hello")    public String hello(){         return "hello Spring Boot";     } }

③ 运行项目

启动项目,在浏览器上访问 http://localhost:8080/hello

搭建步骤:

  1. 在pom文件中添加spring-boot-starter-test测试启动器
  2. 编写单元测试类
  3. 编写单元测试方法
  4. 运行结果

① 在pom文件中添加spring-boot-starter-test测试启动器

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

② 编写单元测试类

@RunWith(SpringRunner.class) @SpringBootTest   public class Chapter01ApplicationTests {     @Test     public void contextLoads() {     } }

③ 编写单元测试方法

@Autowired private HelloController helloController; @Test public void helloControllerTest() {     String hello = helloController.hello();     System.out.println(hello); }

④ 运行结果

       执行测试方法helloControllerTest(),控制台输出如图。

热部署

搭建步骤:

  1. 在pom文件中添加spring-boot-devtools热部署依赖
  2. IDEA中热部署设置
  3. 热部署测试

① 在pom文件中添加spring-boot-devtools热部署依赖

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

②  IDEA中热部署设置

 选择【File】→【Settings】选项,打开Compiler面板设置页。

     使用快捷键“Ctrl+Shift+Alt+/”打开Maintenance选项框,选中并打开Registry页面。

启动chapter01项目,通过浏览器访问http://localhost:8080/hello

修改类HelloController中的请求处理方法hello()的返回值,刷新浏览器

Spring Boot 依赖管理

spring-boot-starter-web依赖

spring-boot-starter-parent依赖

dependency>    

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

spring-boot-starter-web依赖启动器的主要作用是提供Web开发场景所需的底层所有依赖文件,它对Web开发场景所需的依赖文件进行了统一管理。

Spring Boot自动配置的实现

Spring Boot应用的启动入口是@SpringBootApplication注解标注类中的main()方法; @SpringBootApplication能够扫描Spring组件并自动配置Spring Boot。 @SpringBootApplication注解是一个组合注解,包含@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三个核心注解

Spring Boot 的执行流程主要分为两步:

(1)初始化Spring Application实例

(2)初始化Spring Boot项目启动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值