当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot之简单入门

SpringBoot之简单入门

2018年09月17日  | 移动技术网IT编程  | 我要评论

妆遍天下,东海龙网同花顺,湖南顾邦律师网

转发请注明出处:https://www.cnblogs.com/zhangchengzi/p/9661497.html

一,spring boot 是什么?

  spring boot的官网是这样说的:

    spring boot makes it easy to create stand-alone, production-grade spring based applications that you can "just run".

  百度翻译后的意思是:spring bug很容易创建独立的、生产级的基于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依赖。接下来会重新写一篇整合mybatis的示例文章。

        

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

        

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

        

 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 public class indexcontroller {
 9     @requestmapping("/index")
10     @responsebody
11     public string index() {
12         return "spring boot say hello world!";
13     }
14 }

     7.进入learnspringbootapplication类中,鼠标右键:run as -> spring boot app。项目就开始启动

      

     8.如图所示项目启动成功,而且大家有没有发现,在整个过程中都没有配置容器?没错,在传统的ssh或者是ssm框架中,想要测试就必须配置一个容器,例如tomcat。但是在spring boot 中并不需要,因为spring boot自带一个tomcat容器,就像文章开始的时候讲到的,我们要做的就是“just run”。而且从启动日志中我们也可以看到tomcat启动的字样。ok。项目启动成功了,接下来就是测试。打开你最喜欢的浏览器,访问:http://www.lhsxpumps.com/_localhost:8080/index。

   

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

        

     10,示例项目代码,我已经上传到了我的github,大家有需要的话,可以去clone。

        项目github地址:https://github.com/zcc1/learnspringboot2

        

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网