SpringBoot之简单入门

一,spring boot 是什么?html

  spring boot的官网是这样说的:git

    Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".github

  百度翻译后的意思是:Spring boot很容易建立独立的、生产级的基于Spring的应用程序,您能够“只运行”。web

  回想一下咱们在项目中是用spring的过程,须要写不少配置,例如web.xml的配置,数据库配置,事物的配置等等。。。,而spring boot替咱们简化了这些配置,咱们仅仅须要作的就是“just run”,直接使用。spring

  废话再也不多说,咱们这就开始一个入门的示例。数据库

  二,spring boot 入门示例:浏览器

    1,使用到的工具:tomcat

    1. eclipse photon
    2. spring Tools(aka Spring IDE and Spring Tool Suite) 3.9.5.RELEASE:用来快速搭建spring boot。若是你没有安装这个插件,能够百度“spring tool suite 安装”。
    3. jdk 1.8

    2,spring boot 示例搭建步骤:app

      1.依次点击 File -> New -> Other。选择 Spring Starter Project ,而后Next框架

        

      2.填写项目信息,而后Next

        

      3.选择Spring boot版本,在编写这篇文章的时候,spring boot的最新版本是2.0.5,因此就使用了这个版本。

        

      4.选择项目依赖,由于本片文章只是一个spring boot简单示例,因此只选择了一个web依赖。复杂一些的情查阅SpringBoot之整合Mybatis

示例文章。

        

      5.最后点击Finish。等待一会,项目就建立完成了。建立好的项目目录以下:

        

      6.接下来开始编写示例代码,首先建立一个controller包。在新增的controller包中,新建一个IndexController类。IndexController类的代码以下:

       类中用到了@Controller,@RequestMapping,@ResponseBody注解,若是你不了解他们的用法和意义的话能够参考 经常使用注解记录

 1 package com.zcz.controller;  2 
 3 import org.springframework.stereotype.Controller;  4 import org.springframework.web.bind.annotation.RequestMapping;  5 import org.springframework.web.bind.annotation.ResponseBody;  6 
 7 @Controller  8 @RequestMapping("/")  9 public class IndexController { 10     @RequestMapping("/index") 11  @ResponseBody 12     public String index() { 13         return "spring boot say hello world!"; 14  } 15 }

 

     7.进入LearnSpringBootApplication类中,鼠标右键:Run as -> Spring Boot App。项目就开始启动

      

     8.如图所示项目启动成功,并且你们有没有发现,在整个过程当中都没有配置容器?没错,在传统的ssh或者是ssm框架中,想要测试就必须配置一个容器,例如Tomcat。可是在spring boot 中并不须要,由于spring boot自带一个tomcat容器,就像文章开始的时候讲到的,咱们要作的就是“just run”。并且从启动日志中咱们也能够看到tomcat启动的字样。OK。项目启动成功了,接下来就是测试。打开你最喜欢的浏览器,访问:http://localhost:8080/index。

   

    9.你的spring boot 示例项目就这样成功了。项目最终的目录结构以下图

        

     10,示例项目代码,我已经上传到了个人github,你们有须要的话,能够去clone。

        项目github地址:https://github.com/ZCC1/LearnSpringBoot2


原创不易,转发请注明出处:https://www.cnblogs.com/zhangchengzi/p/9661497.html

相关文章
相关标签/搜索