《01.Spring Boot实战:Spring Boot入门篇》

读者也可以关注作者微信公众号连接:https://mp.weixin.qq.com/s?__biz=MzI0NzM4ODE4NQ==&mid=2247483684&idx=1&sn=9d72f9aa4fb0de5f9ea700ed5d2304aa&chksm=e9b18052dec60944079a62268773fb002824367a2ccb6a0c13176cfd74050a390ecd639c7651#rd

温馨提示:本案例中的所有代码,读者可通过左右滑动进行浏览。

1 Spring Boot的概述

Spring Boot是开发者和Spring 本身框架的中间层,帮助开发者统筹管理应用的配置,提供基于实际开发中常见配置的默认处理(即习惯优于配置),简化应用的开发,简化应用的运维;总的来说,其目的Spring Boot就是为了对Java web 的开发进行“简化”和加“快”速度,简化开发过程中引入或启动相关Spring 功能的配置。这样带来的好处就是降低开发人员对于框架的关注点,可以把更多的精力放在自己的业务代码上。

同时随着微服务概念的推广和实践,Spring Boot的精简理念又使其成为Java微服务开发的不二之选,也可以说,Spring Boot其实就是为了微服务而生的Java web框架。现如今,Spring Boot已经在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

2 Spring Boot的核心功能

  • 可独立运行的Spring项目:Spring Boot可以以jar包的形式独立运行。

  • 内嵌的Servlet容器:Spring Boot可以选择内嵌Tomcat、Jetty或者Undertow,无须以war包形式部署项目。

  • 简化的Maven配置:Spring提供推荐的基础 POM 文件来简化Maven 配置。

  • 自动配置Spring:Spring Boot会根据项目依赖来自动配置Spring 框架,极大地减少项目要使用的配置。

  • 提供生产就绪型功能:提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。

  • 无代码生成和xml配置:Spring Boot不生成代码。完全不需要任何xml配置即可实现Spring的所有配置。

  • Spring Boot整合了诸多技术框架,通过配置即可自动关联以及配置与其他框架整合。

  • 使用微信公众号编辑器有一个十分头疼的问题——粘贴出来的代码,格式错乱,而且特别丑。这块编辑器能够解决这个问题。

3.入门案例

(1)在Eclipse开发工具中整合Maven(读者亦可联系作者获取Maven框架使用的教程)(2)在Eclipse中新建一个Mavan的web项目,取名为hellotest


(3)在pom.xml文件中加入如下配置:

 
 
  1. <!-- 加入本配置意味着Spring Boot会自动选择合适的-->

  2. <parent>

  3.   <groupId>org.springframework.boot</groupId>

  4.   <artifactId>spring-boot-starter-parent</artifactId>

  5.   <version>1.5.6.RELEASE</version>

  6. </parent>

构建基于Spring Boot的应用首先必须要将元素设置为spring boot的spring-boot-starter-parent,spring-boot-starter-parent是Spring Boot的核心启动器,包含了自动配置、日志和YAML等大量默认的配置,大大简化了我们的开发。元素建议使用最新的 RELEASE 版本,之后的spring boot模块(如spring-boot-starter-web模块)都会自动选择最合适的版本进行添加,后续的配置基本无需程序员自己制定版本号了。(4)在pom.xml文件中加入如下配置:

 
 
  1. <!-- 加入本配置意味着Spring Boot会自动选择合适的-->

  2. <dependency>

  3.    <groupId>org.springframework.boot</groupId>

  4.    <artifactId>spring-boot-starter-web</artifactId>

  5. </dependency>

Spring Boot中包含了很多的starter模块,简单的说,每一个starter模块就是一系列的依赖包组合。例如starter-web模块,就是包含了Spring Boot预定义的一些Web开发的常用依赖包,支持全栈式Web开发,包括Tomcat和spring-webmvc,同时Spring Boot会进行自动配置集成SpringMVC。由于指定了spring-boot-starter-parent,所以此处的web starter模块不需要指定版本,Spring Boot会自动选择最合适的版本进行添加。修改完成之后的pom.xml文件内容如下:

 
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2.  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3.  <modelVersion>4.0.0</modelVersion>

  4.  <groupId>org.fkit.springboot</groupId>

  5.  <artifactId>hellotest</artifactId>

  6.  <version>0.0.1-SNAPSHOT</version>

  7.  <packaging>jar</packaging>

  8.  <name>hellotest</name>

  9.  <url>http://maven.apache.org</url>

  10.   <parent>

  11.        <groupId>org.springframework.boot</groupId>

  12.        <artifactId>spring-boot-starter-parent</artifactId>

  13.        <version>1.5.6.RELEASE</version>

  14.   </parent>

  15.    <properties>

  16.        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  17.    </properties>

  18.    <dependencies>

  19.        <dependency>

  20.            <groupId>org.springframework.boot</groupId>

  21.            <artifactId>spring-boot-starter-web</artifactId>

  22.    </dependency>

  23.    <dependency>

  24.          <groupId>junit</groupId>

  25.          <artifactId>junit</artifactId>

  26.          <version>3.8.1</version>

  27.          <scope>test</scope>

  28.      </dependency>

  29.    </dependencies>

  30. </project>

pom.xml文件修改保存后,Maven会自动下载所需要的所有jar文件,速度取决于读者网络。(5)编写测试代码a.写一个简单的Java类HelloController。

 
 
  1. // 程序清单:codes/01/hellotest/org/fkit /hellotest/HelloController.java

  2. import org.springframework.web.bind.annotation.RequestMapping;

  3. import org.springframework.web.bind.annotation.RestController;

  4. // RestController相当于SpringMVC中的 @Controller + @ResponseBody

  5. @RestController

  6. public class HelloController {

  7.    // 映射"/hello"请求

  8.    @RequestMapping("/hello")

  9.    public String hello(){

  10.        return " Hello Spring Boot!";

  11.    }

  12. }

  13. HelloController 类上使用的@RestController注解是一个组合注解,相当于SpringMVC中的@Controller+@ResponseBody合在一起的作用。表明这个请求返回字符串“hello”。

b.修改Maven默认的App类。

 
 
  1. // 程序清单:codes/01/hellotest/org/fkit/hellotest/App.java

  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. // @SpringBootApplication指定这是一个 spring boot的应用程序.

  5. @SpringBootApplication

  6. public class App

  7. {

  8.    public static void main( String[] args )

  9.    {

  10.        // SpringApplication 用于从main方法启动Spring应用的类。

  11.        SpringApplication.run(App.class, args);

  12.    }

  13. }

App类使用的@ SpringBootApplication注解指定这是一个 spring boot的应用程序,该注解也是一个组合注解,相当于@Configuration + @EnableAutoConfiguration + @ComponentScan,具体在后续《Spring Boot的核心》重点讲解。SpringApplication类 用于从main方法启动Spring应用的类。此处直接调用静态的run方法。c.启动Spring Boot项目,右键执行App.java类运行main方法。d.测试应用Spring Boot项目启动后,默认访问地址为:http://localhost:8080/,按照之前的web项目习惯,读者可能会问,怎么没有项目路径?这就是Spring Boot的默认设置了,将项目路径直接设为根路径。在浏览器输入URL来测试应用。

 
 
  1. http://localhost:8080/hello

结果如下:

如有疑问请与读者联系,Spring Boot目前十分火热,后续将继续更新。


SpringBoot实战(第4版)清晰文字版,第 1 章 入门 ................................................ 1 1.1 Spring 风云再起 ........................................ 1 1.1.1 重新认识 Spring ............................ 2 1.1.2 Spring Boot 精要 ........................... 3 1.1.3 Spring Boot 不是什么 ................... 6 1.2 Spring Boot 入门 ....................................... 6 1.2.1 安装 Spring Boot CLI .................... 7 1.2.2 使用 Spring Initializr 初始化 Spring Boot 项目 .......................... 10 1.3 小结 ......................................................... 18 第 2 章 开发第一个应用程序 .................... 19 2.1 运用 Spring Boot ..................................... 19 2.1.1 查看初始化的 Spring Boot 新项目 .......................................... 21 2.1.2 Spring Boot 项目构建过程 解析 .............................................. 24 2.2 使用起步依赖 .......................................... 27 2.2.1 指定基于功能的依赖 ................... 28 2.2.2 覆盖起步依赖引入的传递依赖 .... 29 2.3 使用自动配置 .......................................... 30 2.3.1 专注于应用程序功能 ................... 31 2.3.2 运行应用程序 .............................. 36 2.3.3 刚刚发生了什么 ........................... 38 2.4 小结 ......................................................... 41 第 3 章 自定义配置 .................................... 42 3.1 覆盖 Spring Boot 自动配置 ..................... 42 3.1.1 保护应用程序 .............................. 43 3.1.2 创建自定义的安全配置 ............... 44 3.1.3 掀开自动配置的神秘面纱 ........... 48 3.2 通过属性文件外置配置 ........................... 49 3.2.1 自动配置微调 .............................. 50 3.2.2 应用程序 Bean 的配置外置 ......... 55 3.2.3 使用 Profile 进行配置 .................. 59 3.3 定制应用程序错误页面 ........................... 62 3.4 小结 ......................................................... 64 第 4 章 测试 ............................................... 66 4.1 集成测试自动配置 .................................. 66 4.2 测试 Web 应用程序 ................................. 68 4.2.1 模拟 Spring MVC ........................ 69 4.2.2 测试 Web 安全 ............................. 72 4.3 测试运行中的应用程序 ........................... 74 4.3.1 用随机端口启动服务器 ............... 75 4.3.2 使用 Selenium 测试 HTML 页面 ............................................. 76 4.4 小结 ......................................................... 78 第 5 章 Groovy 与 Spring Boot CLI ......... 80 5.1 开发 Spring Boot CLI 应用程序 .............. 80 5.1.1 设置 CLI 项目 .............................. 81 5.1.2 通过 Groovy 消除代码噪声 ......... 81 5.1.3 发生了什么 .................................. 85 5.2 获取依赖 .................................................. 86 5.2.1 覆盖默认依赖版本 ....................... 87 5.2.2 添加依赖仓库 .............................. 88 5.3 用 CLI 运行测试 ...................................... 89 5.4 创建可部署的产物 .................................. 91 5.5 小结 ......................................................... 91 第 6 章 在 Spring Boot 中使用 Grails ...... 93 6.1 使用 GORM 进行数据持久化 ................. 93 2 目 录 6.2 使用 Groovy Server Pages 定义视图 ....... 98 6.3 结合 Spring Boot 与 Grails 3 ................. 100 6.3.1 创建新的 Grails 项目 ................. 100 6.3.2 定义领域模型 ............................ 103 6.3.3 开发 Grails 控制器 ..................... 104 6.3.4 创建视图 .................................... 105 6.4 小结 ....................................................... 107 第 7 章 深入 Actuator .............................. 108 7.1 揭秘 Actuator 的端点 ............................ 108 7.1.1 查看配置明细 ............................ 109 7.1.2 运行时度量 ................................ 115 7.1.3 关闭应用程序 ............................ 121 7.1.4 获取应用信息 ............................ 121 7.2 连接 Actuator 的远程 shell .................... 122 7.2.1 查看 autoconfig 报告 ........... 123 7.2.2 列出应用程序的 Bean ............... 124 7.2.3 查看应用程序的度量信息 ......... 124 7.2.4 调用 Actuator 端点 .................... 125 7.3 通过 JMX 监控应用程序 ....................... 126 7.4 定制 Actuator......................................... 128 7.4.1 修改端点 ID ............................... 128 7.4.2 启用和禁用端点 ........................ 129 7.4.3 添加自定义度量信息 ................. 129 7.4.4 创建自定义跟踪仓库 ................. 132 7.4.5 插入自定义健康指示器 ............. 134 7.5 保护 Actuator 端点 ................................ 136 7.6 小结 ....................................................... 138 第 8 章 部署 Spring Boot 应用程序 ........ 139 8.1 衡量多种部署方式 ................................ 139 8.2 部署到应用服务器 ................................ 140 8.2.1 构建 WAR 文件 ......................... 141 8.2.2 创建生产 Profile ........................ 142 8.2.3 开启数据库迁移 ........................ 145 8.3 推上云端 ............................................... 150 8.3.1 部署到 Cloud Foundry ............... 150 8.3.2 部署到 Heroku ........................... 153 8.4 小结 ....................................................... 155 附录 A Spring Boot 开发者工具.............. 157 附录 B Spring Boot 起步依赖 ................. 163 附录 C 配置属性 ...................................... 169 附录 D Spring Boot 依赖 ......................... 202
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值