Spring Boot 快速搭建

       本章主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。

一、系统与工具要求

  • Java 7及以上
  • Spring Framework 4.3.16及以上
  • Maven 4
  • 集成开发工具:IntelliJ IDEA 2016 及以上
  • Spring Boot采用的是 2.1.2.RELEASE

二、创建项目方式

  1. 可以访问:http://start.spring.io创建
  2. 可以通过集成开发工具

三、访问网址创建

     1  通过SPRING INITIALIZR工具产生基础项目

1.1 访问官网提供的构建网址  http://start.spring.io/ 

1.2 选择构建工具Maven Project、Spring Boot版本2.1.2 以及一些工程基本信息,可参考下图

        

springboot如何建立一个持久的Socket连接 springboot快速搭建_xml

1.3 点击Generate Project下载项目压缩包

 

     2 解压项目包,并用IDE以Maven项目导入,以IntelliJ IDEA 17为例:

2.1 菜单中选择File–>New–>Project from Existing Sources...

2.2 选择解压后的项目文件夹,点击OK

2.3 点击Import project from external model并选择Maven,点击Next到底为止。

2.4 若你的环境有多个版本的JDK,注意到选择Java SDK的时候请选择Java 7以上的版本

     项目解析

springboot如何建立一个持久的Socket连接 springboot快速搭建_spring_02

       通过上面步骤完成了基础项目的创建,如上图所示,Spring Boot的基础结构共三个文件(具体路径根据用户生成项目时填写的Group所有差异):

  • src/main/java下的程序入口:Chapter1Application
  • src/main/resources下的配置文件:application.properties
  • src/test/下的测试入口:Chapter1ApplicationTests

生成的Chapter1ApplicationChapter1ApplicationTests类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。

四、集成工具Idea创建

     1 创建 Spring Initializr 项目 

1.1、点击File -> Project, 如图

springboot如何建立一个持久的Socket连接 springboot快速搭建_spring_03

1.2 选择Spring Initializr

            如图下图中选项的是Spring Initializr(官方的构建插件,需要联网),第二个是自己选择Maven构建,为了更好的学习,我们将在本章用插件构建:

springboot如何建立一个持久的Socket连接 springboot快速搭建_Java_04

1.3 填写项目基本信息

            如下图中,Group: 组织ID,一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为 org、com、cn等等,其中 org为非营利组织,com为商业组织。如阿里、淘宝(com.alibaba/com.taobao)

Artifact: 唯一标识符,一般是项目名称

springboot如何建立一个持久的Socket连接 springboot快速搭建_spring_05

1.4 选择包和项目使用的技术

            Spring Initializr 为我们提供了很多的选项,不同的选项有不同的作用,在初期我们只需要依赖Web -> Web 就可以了,选择好依赖包之后点击Next -> Finish

springboot如何建立一个持久的Socket连接 springboot快速搭建_Spring Boot_06

        2 目录结构

 

springboot如何建立一个持久的Socket连接 springboot快速搭建_spring_02

 

五  pom.xml依赖

1 模块的引入,对web和test的支持

      1.1 通过 方法一 创建的项目的pom.xml引入了两个模块

springboot如何建立一个持久的Socket连接 springboot快速搭建_spring_08

    没有引入web模块,需要引入web模块

springboot如何建立一个持久的Socket连接 springboot快速搭建_Java_09

     1.2 通过方法二 idea创建 pom.xml引入的模块

springboot如何建立一个持久的Socket连接 springboot快速搭建_xml_10

2 设置spring boot 的parent

springboot如何建立一个持久的Socket连接 springboot快速搭建_Spring Boot_11

Spring boot的项目必须要将parent设置为spring boot的parent,该parent包含了大量默认的配置,大大简化了我们的开发

3 Spring Boot 插件

springboot如何建立一个持久的Socket连接 springboot快速搭建_xml_12

六 编写简单web服务案例

1 Spring Boot的项目主函数入口

注意事项:一个项目中切记不要出现多个 main 函数,否在在打包的时候 spring-boot-maven-plugin 将找不到主函数(主动指定打包主函数入口除外…)

使用上述方法新建Spring Boot项目后,生成的项目的根包目录下会有 artifactld+Application命名规则的入口类,入口类要在所有java包的顶层。如图

springboot如何建立一个持久的Socket连接 springboot快速搭建_spring_13

2 添加测试器控制类

创建package为com.sxb.chapter1.controller(根据实际情况修改)

创建HelloWorldController类,内容如下

package com.sxb.chapter1.controller;

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

/**
 * 功能描述:
 *
 * @date: 2019年02月18日 15:42
 * @author: xbShi
 * @version: 1.0
 */
@RestController
public class HelloWorldController {

    @RequestMapping("/hello")
    public String helloWord() {
        return "Hello Spring Boot!";
    }
}

3 项目主程序

springboot如何建立一个持久的Socket连接 springboot快速搭建_Java_14

启动主程序main方法

springboot如何建立一个持久的Socket连接 springboot快速搭建_spring_15

控制台启动效果

springboot如何建立一个持久的Socket连接 springboot快速搭建_spring_16

4 浏览器测试

输入地址 localhost:8080/hello

运行效果

springboot如何建立一个持久的Socket连接 springboot快速搭建_Java_17

到此Spring Boot项目就创建好了,一个简单的web程序完成。

 

上一篇:Sping Boot 系列一:Spring Boot 初认识

下一篇:Spring Boot系列三:Spring Boot的入口类和@SpringBootApplication