本文记录了SpringBoot入门的过程,开发工具是IDEA,使用gradle来构建项目。
首先学习一个新东西,最好的地方就是他的 官方网站。
1.首先在电脑上安装好运行环境,JDK1.8及以上,gradle3.4及以上,IDEA开发工具。
2.打开IDEA,选择新建项目,按下图选择gradle,java,web,点next下去
(其实也能够直接选Spring Initializr,而后选须要的功能,next下去,直接生成SpringBoot项目)
这里gradle我选择了本地安装的,不用再下载了 java
3.项目建好以后,打开builde.gradle文件,输入配置导入SpringBoot依赖包之类的,保存以后gradle就会自动下载依赖包构建项目了,就是下图这个界面
web
group 'SpringBootDemo1' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'war' apply plugin: 'org.springframework.boot' sourceCompatibility = 1.8 buildscript { repositories { jcenter() maven { url 'http://repo.spring.io/snapshot' } maven { url 'http://repo.spring.io/milestone' } } dependencies { classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.0.0.BUILD-SNAPSHOT' } } jar { baseName = 'myproject' version = '0.0.1-SNAPSHOT' } repositories { jcenter() maven { url "http://repo.spring.io/snapshot" } maven { url "http://repo.spring.io/milestone" } } dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile("org.springframework.boot:spring-boot-starter-test") }
4.gradle成功运行完毕后,找到src-main-java文件建,右键点击新建一个Application类。 以下图所示
下面咱们就能够开始写第一个SpringBoot程序:Hello Worldspring
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by zhangyi on 2017/3/29. */ @EnableAutoConfiguration @RestController public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } @RequestMapping("/") public String home(){ return "Hello World ! "; } }
5.代码写好以后,就是运行程序了,在main方法的左边,有一个绿色的小三角形,点击它,而后选择Run Application ,本身的Web服务器就跑起来了,到这里是否是有点小激动呢
而后IDEA下面会输出启动信息
若是在日志的最后面看到Started Application in这样的信息,就说明本身的web服务器已经成功跑起来了。
最后,打开本身最喜欢的浏览器,输入访问路径localhost:8080/ ,就能够看到下面这样的输出
有没有发现本身其实并无写多少东西,一个本身的Web服务器就在本地跑起来了呢,这就是SpringBoot的特色之一,极少的配置,极大简便了Java Web的开发,哈哈,真棒!浏览器