关于程序员:Spring-Boot-Hello-World-基于-IDEA-案例详解

23次阅读

共计 3247 个字符,预计需要花费 9 分钟才能阅读完成。

一、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_b…

二、Spring Boot Hello World 实战详解

首先得有个 maven 根底我的项目,能够间接应用 Maven 骨架工程生成 Maven 骨架 Web 我的项目,即 man archetype:generate 命令:

mvn archetype:generate -DgroupId=springboot -DartifactId=springboot-helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2.1 pom.xml 配置

代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springboot</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-helloworld :: HelloWorld Demo</name>

    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Spring Boot web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

只有退出一个 Spring Boot 启动父依赖即可。

2.2 Controller 层

HelloWorldController 的代码如下:

/**
 * Spring Boot HelloWorld 案例
 *
 * Created by bysocket on 16/4/26.
 */
@RestController
public class HelloWorldController {@RequestMapping("/")
    public String sayHello() {return "Hello,World!";}
}

@RestController 和 @RequestMapping 注解是来自 SpringMVC 的注解,它们不是 SpringBoot 的特定局部。
1.@RestController:提供实现了 REST API,能够服务 JSON,XML 或者其余。这里是以 String 的模式渲染出后果。

  1. @RequestMapping:提供路由信息,”/“门路的 HTTP Request 都会被映射到 sayHello 办法进行解决。具体参考,世界上最好的文档起源自官网的《Spring Framework Document》

2.3 Spring Boot 启动利用类

和第一段形容一样,开箱即用。如上面 Application 类:

/**
 * Spring Boot 利用启动类
 *
 * Created by bysocket on 16/4/26.
 */
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);
    }
}
  1. @SpringBootApplication:Spring Boot 利用的标识
  2. Application 很简略,一个 main 函数作为主入口。SpringApplication 疏导利用,并将 Application 自身作为参数传递给 run 办法。具体 run 办法会启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件。

2.4 Controller 层测试类

一个好的程序,不能短少好的 UT。针对 HelloWorldController 的 UT 如下:

/**
 * Spring Boot HelloWorldController 测试 - {@link HelloWorldController}
 *
 * Created by bysocket on 16/4/26.
 */
public class HelloWorldControllerTest {
 
    @Test
    public void testSayHello() {assertEquals("Hello,World!",new HelloWorldController().sayHello());
    }
}

三、Spring Boot Hello World 案例运行

Just Run 的主旨,运行很简略,间接右键 Run 运行 Application 类。同样你也能够 Debug Run。能够在控制台中看到:

Tomcat started on port(s): 8080 (http)
Started Application in 5.986 seconds (JVM running for 7.398)

而后拜访 http://localhost:8080/ , 即可在页面中看到 Spring Boot 对你 say hello:
Hello,World!

四、小结

  1. Spring Boot pom 配置
  2. Spring Boot 启动及原理

如以上文章或链接对你有帮忙的话,别忘了在文章结尾处评论哈~ 你也能够点击页面左边“分享”悬浮按钮哦,让更多的人浏览这篇文章。

出处:公号「程序员泥瓦匠」
博客:https://bysocket.com/

内容涵盖 Java 后端技术、Spring Boot、Spring Cloud、微服务架构、运维开发、系统监控等相干的钻研与常识分享。

正文完
 0