c1-gettting-starter.zip

写于:2018-12-03 05:50:37 2020年 文章进行升级,相关版本为:2.1.5.RELEASE

一、Introduce

Spring-Boot 官网介绍

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Spring boot 是 在 Spring framwork 之上的封装。Spring boot 将所有日常通用配置已经写好,用户只需要很少的配置,如果不需要定制,几乎可以说是0配置方式,直接启动一个具有各种功能的应用程序。

Spring Boot 是构建微服务的底层依赖组件,Spring Boot创建的应用,通过内嵌的 容器(tomcat 等)启动,通过 启动类,或者打包成 jar 包,通过java -jar xx.jar 便能够简单便捷的启动应用。

Spring Boot 开箱即用、便捷、丰富的第三方支持、自动化配置的优点,深受广大开发者喜爱。其配套的 Spring Cloud 为中小企业提供了一整套完整的分布式解决方案。

Spring Boot 的发展非常快速,目前已经2.x版本。我所在公司有一些应用使用的认识 1.x 版本,且1.x 和 2.x 的API也相差较大。同时 Spring-Cloud 技术扩展更迭也很快,例如在 Eureka服务发现,Netflix 宣布闭源,服务熔断 hystrix 停止开发维护不久,Spring Cloud Alibaba 就跟着出现了,提供了如 nacos 等的替代方案。

总的来说,Spring Boot 开箱即用给开发带来了很多好处,不过 spring Boot 简单便捷的外表下确实一整套复杂的运行体系,需要开发人员深入底层的 spring framework 和 spring boot 本身的运行机制进行深入学习,才能够对 spring Boot 乃至 Spring Cloud 的使用得心应手。

二、Getting Starter

直接通过 Eclipse / 或者 IDEA 提供的 Spring Boot 快速创建工具创建 Spring Boot 项目

1、创建一个 Spring Boot Web 项目,并启动测试

目录结构:
01.png

  • step1、使用idea 创建 项目,并需要提供如下组件

    spring-boot-starter-web : Web 全栈开发依赖 Spring Boot stater 依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.xxx</groupId>
  12. <artifactId>c1-gettting-starter</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>c1-gettting-starter</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <!-- web 组件 -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <plugins>
  28. <plugin>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-maven-plugin</artifactId>
  31. </plugin>
  32. </plugins>
  33. </build>
  34. </project>
  • step2、编写配置文件application.properties (暂时为空)

默认端口号为:8080

  • step3、编写启动类 Application.java

    1. @SpringBootApplication // 启动类标注
    2. @RestController // Spring MVC 控制器 标注
    3. public class Application {
    4. public static void main(String[] args) {
    5. // 真实启动的方法
    6. SpringApplication.run(Application.class, args);
    7. }
    8. /** web 服务测试 **/
    9. @RequestMapping("/hello")
    10. public String hello(){
    11. return "hello";
    12. }
    13. }
  • step4、直接启动 run 方法

02.png
访问 http://localhost:8080/hello

  1. $ curl http://localhost:8080/hello
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 5 100 5 0 0 5000 0 --:--:-- --:--:-- --:--:-- 5000
  5. hello