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

  本篇会经过完成一个经常使用的Helloworld示例,来开始咱们的Spring Boot旅程html

准备工做

15分钟java

开发环境web

项目建立

  项目建立的时候咱们能够选择经过自定义配置Maven文件来建立,也能够用Spring官方提供的Spring Initializr工具,Spring Initializr是一个能够简化Spring Boot项目构建的工具,有两种使用Spring Initializr的方法:spring

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

  • 开发工具集成的方式浏览器

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

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

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

  看一下项目建立完成以后的结构:app

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;

@RestController
public 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

欢迎关注个人微信公众号:

相关文章
相关标签/搜索