Spring Boot Hello World 基于 IDEA 案例详解

[复制链接]
发表于 2023-2-21 14:24:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
一、Spring Boot 是什么

世界上最好的文档来源自官方的《Spring Boot Reference Guide》,是这样介绍的:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”...Most Spring Boot applications need very little Spring configuration.
Spring Boot(英文中是“引导”的意思),是用来简化Spring应用的搭建到开发的过程。应用开箱即用,只要通过 “just run”(可能是 java -jar 或 tomcat 或 maven插件run 或 shell脚本),就可以启动项目。二者,Spring Boot 只要很少的Spring配置文件(例如那些xml,property)。 因为“习惯优先于配置”的原则,使得Spring Boot在快速开发应用和微服务架构实践中得到广泛应用。   Javaer装好JDK环境和Maven工具就可以开始学习Boot了~
原文链接:https://bysocket.com/spring_boot_hello_world/
二、Spring Boot Hello World 实战详解

首先得有个 maven 基础项目,可以直接使用 Maven 骨架工程生成 Maven 骨架 Web 项目,即 man archetype:generate 命令:
  1. mvn archetype:generate -DgroupId=springboot -DartifactId=springboot-helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
复制代码
2.1  pom.xml配置

代码如下:
  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <groupId>springboot</groupId>
  6.     <artifactId>springboot-helloworld</artifactId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <name>springboot-helloworld :: HelloWorld Demo</name>
  9.    
  10.     <parent>
  11.         <groupId>org.springframework.boot</groupId>
  12.         <artifactId>spring-boot-starter-parent</artifactId>
  13.         <version>1.3.3.RELEASE</version>
  14.     </parent>
  15.     <dependencies>
  16.         
  17.         <dependency>
  18.             <groupId>org.springframework.boot</groupId>
  19.             <artifactId>spring-boot-starter-web</artifactId>
  20.         </dependency>
  21.         
  22.         <dependency>
  23.             <groupId>junit</groupId>
  24.             <artifactId>junit</artifactId>
  25.             <version>4.12</version>
  26.         </dependency>
  27.     </dependencies>
  28. </project>
复制代码
只要加入一个 Spring Boot 启动父依赖即可。
2.2 Controller 层

HelloWorldController的代码如下:
  1. /**
  2. * Spring Boot HelloWorld案例
  3. *
  4. * Created by bysocket on 16/4/26.
  5. */
  6. @RestController
  7. public class HelloWorldController {
  8.     @RequestMapping("/")
  9.     public String sayHello() {
  10.         return "Hello,World!";
  11.     }
  12. }
复制代码
@RestController和@RequestMapping注解是来自SpringMVC的注解,它们不是SpringBoot的特定部分。
1.@RestController:提供实现了REST API,可以服务JSON,XML或者其他。这里是以String的形式渲染出结果。
2. @RequestMapping:提供路由信息,"/“路径的HTTP Request都会被映射到sayHello方法进行处理。具体参考,世界上最好的文档来源自官方的《 Spring Framework Document》
2.3 Spring Boot 启动应用类

和第一段描述一样,开箱即用。如下面Application类:
  1. /**
  2. * Spring Boot应用启动类
  3. *
  4. * Created by bysocket on 16/4/26.
  5. */
  6. @SpringBootApplication
  7. public class Application {
  8.     public static void main(String[] args) {
  9.         SpringApplication.run(Application.class,args);
  10.     }
  11. }
复制代码

  • @SpringBootApplication:Spring Boot 应用的标识
  • Application很简单,一个main函数作为主入口。SpringApplication引导应用,并将Application本身作为参数传递给run方法。具体run方法会启动嵌入式的Tomcat并初始化Spring环境及其各Spring组件。
2.4 Controller 层测试类

一个好的程序,不能缺少好的UT。针对HelloWorldController的UT如下:
  1. /**
  2. * Spring Boot HelloWorldController 测试 - {@link HelloWorldController}
  3. *
  4. * Created by bysocket on 16/4/26.
  5. */
  6. public class HelloWorldControllerTest {
  7.     @Test
  8.     public void testSayHello() {
  9.         assertEquals("Hello,World!",new HelloWorldController().sayHello());
  10.     }
  11. }
复制代码
三、Spring Boot Hello World 案例运行

Just Run的宗旨,运行很简单,直接右键Run运行Application类。同样你也可以Debug Run。可以在控制台中看到:
  1. Tomcat started on port(s): 8080 (http)
  2. Started Application in 5.986 seconds (JVM running for 7.398)
复制代码
然后访问  http://localhost:8080/ ,即可在页面中看到Spring Boot对你 say hello:
Hello,World!
四、小结


  • Spring Boot pom配置
  • Spring Boot 启动及原理
如以上文章或链接对你有帮助的话,别忘了在文章结尾处评论哈~ 你也可以点击页面右边“分享”悬浮按钮哦,让更多的人阅读这篇文章。
出处:公号「程序员泥瓦匠」
博客: https://bysocket.com/
内容涵盖 Java 后端技术、Spring Boot、Spring Cloud、微服务架构、运维开发、系统监控监控等相关的研究与知识分享。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表