版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Phapha1996/article/details/78515865
一、SpringBoot是什么
springboot是一款最近比较火热的微框架,在web开发中,它对spring组建更加便捷化,去除了冗长的xml配置,以“约定命名俗称”的规则,最大力度的简化配置,使用便捷流行的注解开发。它是基于Spring基础源码构建起来的,如今微服务、云服务兴起,SpringBoot不仅仅能够简化web开发,还对这些服务具有极好的支持,对分布式开发其也有自己的一套。SpringBoot内嵌有Web容器,默认是Tomcat,与Spring各类套件(如SpringMvc/security)有非常好的支持。还在等什么,开始学习吧,本篇对SpringBoot做基础入门,下一篇为springboot操作数据库。
二、开始实践
1、
项目采用maven构建,使用myeclipse新建web项目,选择add maven support,一路next,配置好各groupid、afreidid后完成构建。
2、在pom.xml中导入依赖与集成SpringBoot parent以获得支持,具体代码块如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<springBoot.groupId>org.springframework.boot</springBoot.groupId>
</properties>
<dependencies>
<dependency>
<groupId>${springBoot.groupId}</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
其中spring-boot-starter-web集成了spring、springMVC等依赖,详情可以查看maven的依赖包中查看导入的包列表。
3.在src/main/resource中创建application.yml文件,配置端口以及编码,该文件是springboot的默认配置文件,springboot会自动读取该文件进行配置(这是springboot默认读取的配置文件,application.yml或者application.properties,在这里只需要做几行简单的配置,我们就可以运用spring中强大的功能),代码如下:
server:
port: 8080
tomcat:
uri-encoding: UTF-8
package org.fage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author Caizhfy
* @email [email protected]
* @createTime 2017年10月30日
* @description 创建步骤:
* 1.先创建maven web项目
* 2.继承parent,增加springboot-web依赖,配置打包标签
* 3.配置application.yml
* 4.创建运行文件App.java
* 5.用java -jar 文件.jar 进行运行
*/
@SpringBootApplication
@RestController
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@RequestMapping("/")
public String hello(){
return "hello-SpringBootApp!!";
}
}
直接运行该主函数,访问127.0.0.1:8080/即可在网页输出字串helloworld。
致此,初步的你已经学会了使用springboot构建简单的web平台,为什么一行注解与一个类就能实现了这样强大的操作?请看后续对springboot做原理的解析吧~
源码地址:https://github.com/Phapha1996/springboot-hello
下一篇:springboot做数据访问