SpringBoot之简单入门

一,spring boot 是什么?

  spring boot的官网是这样说的:

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

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

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

  废话不再多说,我们这就开始一个入门的示例。

  二,spring boot 入门示例:

    1,使用到的工具:

    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 示例搭建步骤:

      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

posted @ 2018-09-17 12:55  张橙子  阅读(921)  评论(0编辑  收藏  举报