spring boot 系列之一:spring boot 入门

 

最近在学习 spring boot,感觉确实很好用,开发环境搭建和部署确实省去了很多不必须要的重复劳动。

接下来就让我们一起来复习下。

一、什么是 spring boot ? spring boot 是干嘛的?

  Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot 致力于在蓬勃发展的快速应用开发领域 (rapid application development) 成为领导者。

二、spring boot 入门实例

  接下来将通过一个 Hello 实例来看下怎么使用 spring boot 搭建一个应用。

  1. 创建 maven 项目
    1.  

    2.  

    3.  

    4. 创建的目录结构

       

  2. 配置 pom 文件
    <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>spring-boot</groupId>
        <artifactId>study</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!-- 指定 parent 项目 -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.10.RELEASE</version>
        </parent>
    
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> spring boot默认的jdk 版本为1.6,我们在这里改为1.8 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.8<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 引入spring-boot-starter-web 依赖 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
            <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 由于在spring boot 顶层POM文件中定义了相关dependencyManagement,因此这里就不需要配置 &lt;version&gt;&lt;/version&gt;, 
                相关说明可以参考 https://blog.csdn.net/liutengteng130/article/details/46991829 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-web<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    

    </project>

    View Code
  3. 创建 controller
    package com.study.controller;
    

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

    /**

    • 这里 @RestController = @ Controller + @ ResponseBody,
    • 会将方法的返回结果直接放入 http 返回报文的正文部分,直接显示到页面

    */
    @RestController
    public class HelloController {

    @RequestMapping(</span>"/hello"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String sayHello() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> "hello ,spring boot"<span style="color: rgba(0, 0, 0, 1)">;
    }
    

    }

    View Code
  4. 创建 app 启动类
    package com.study;
    

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class App {

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
        SpringApplication.run(App.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args);
    }
    

    }

    View Code
  5. 测试

    1. 上述步骤完成之后,还有一个错误需要解决,根据其提示进行修复即可

    

    2. 最终的目录结构如下:

    

 

    3. 运行 App 的 main 方法:Run As-->Java Application

    启动 log 如下:  

    4. 通过浏览器访问,正常返回 controller 中的配置内容

    

三、总结

从上述例子我们看到 spring boot 默认替我们做了一些操作

  1. 嵌入的 Tomcat,无需部署 WAR 文件,默认端口号为 8080
  2. 简化 Maven 配置,自动为我们引入依赖
  3. 自动配置 Spring,省去了我们配置 spring xml 文件的麻烦
  4. 默认我们的项目命名空间为 "/"
  5. 还有一个比较重要但是例子中未能显示体现出来的是:spring boot 默认自动扫描 配置了 @SpringBootApplication 注解的类所在的包及其子包,并且这个类不能放在默认包下,否则会报错

    ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.  并且不能正常启动。