Spring Boot实践教程(一):Hello,world!

来源:互联网 发布:js 数组排序 编辑:程序博客网 时间:2024/05/19 12:38

  本篇会通过完成一个常用的Helloworld示例,来开始我们的Spring Boot旅程

准备工作

15分钟

开发环境

项目创建

  项目创建的时候我们可以选择通过自定义配置Maven文件来创建,也可以用Spring官方提供的Spring Initializr工具,Spring Initializr是一个可以简化Spring Boot项目构建的工具,有两种使用Spring Initializr的方法:

  • 官方提供的web页面工具,地址

  • 开发工具集成的方式

  两种方式操作的步骤是一样的,只不过通过Web页面工具多了一步将生成结果导入到开发工具的过程,这里我们采用的第二种:开发工具集成的Spring Initializr。

  在Idea中New Project >选择Spring Initializ即出现如下对话框:

  填写对应的Group、Artifact,选择Type为Maven Project,并填写其他相关信息(如上图所示)后点击下一步,在这个步骤中可以选择对应的功能,选择后Spring Boot就会为项目添加对应的依赖等,这里我们只添加一个Web的基础功能。进一步填写项目路径后即可完成一个Web项目的创建。

  看一下项目创建完成之后的结构:

spring-boot-1-helloworld    │  pom.xml        ├─src    │  ├─main    │  │  ├─java    │  │  │  └─com    │  │  │      └─practice    │  │  │          └─springboot    │  │  │              └─hello    │  │  │                  │  SpringBoot1HelloworldApplication.java    │  │  │                              │  │  └─resources    │  │      │  application.properties    │  │      ├─static    │  │      └─templates    │  └─test    │      └─java    │          └─com    │              └─practice    │                  └─springboot    │                      └─hello    │                              SpringBoot1HelloworldApplicationTests.java

项目运行

  我们可以通过运行main方法的方式将项目启动起来,我们先创建一个Controller来看更直观的看一下启动之后的效果,在web包(需要手动创建)中创建HelloController:

package com.practice.springboot.hello.web;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {    @RequestMapping("/")    public String hi(){        return "Congratulations! It works!";    }}

  运行Springboot1HelloApplication.java中的main方法,我们会在控制台中看到一个Spring的标志和Spring Boot版本的说明。启动完成之后在浏览器中访问http://localhost:8080/ ,会看到下图。至此我们花了几分钟,就创建了一个web项目并将其运行起来了。在这个过程中我们没有添加任何jar包的依赖,没有配置Web.xml,没有配置Spring和SpringMVC的环境,甚至都没有将项目部署只Tomcat,因为这一切Spring Boot都为我们完成了。

项目分析

  首先我们来看一下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>com.practice.springboot</groupId>    <artifactId>hello</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>spring-boot-1-helloworld</name>    <description>spring-boot-1-helloworld</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.8.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

  在pom文件中可以看到以下信息:
1. 整个项目继承于spring-boot-starter-parent
2. 项目有两个依赖包spring-boot-starter-web,spring-boot-starter-test

  有了以上两个配置,Spring Boot就为我们配置了一个基础的web项目的环境,包括依赖包和一个嵌入的tomcat环境。后续的文章里面我们会分析一下这里的starter。

项目打包

  在项目路径下运行maven命令mvn package,跑完之后就会在项目的target目录下得到一个jar文件,这就是项目包。跟以前的war包不太一样的是,这里的项目包格式是jar包。

  可以在通过java -jar 文件名.jar 来运行这个jar文件,然后在浏览器中访问http://localhost:8080/ ,可以看到结果与之前在开发环境下是一样的。

总结

  通过以上的步骤,完成了一个初步的最基础的Spring Boot的项目的创建和运行,后续我会通过实践的方式来分析一下Spring Boot的一些常用功能和特性,并按照我自己的理解来分析一下Spring Boot的运行原理。

  参考:
  Spring Boot Reference Guide

欢迎关注我的微信公众号:

原创粉丝点击