spring-boot 速成(1) helloworld

本文介绍如何在Mac上安装Spring Boot并快速创建Hello World示例,还提供了使用Gradle构建项目的详细步骤,包括配置文件说明、项目结构展示及运行、打包教程。

一、mac上安装

$ brew tap pivotal/tap
$ brew install springboot

安装成功后,可在终端查看命令行

➜  ~ spring --version
Spring CLI v1.5.2.RELEASE

 

二、极速体验hello world

随便开个vim啥的,敲几行代码:

@RestController
class ThisWillActuallyRun {
    @RequestMapping("/")
    String home() {
        "Hello World!"
    }
}

保存成app.groovy,然后在终端下就可以运行了:

spring run app.groovy

不要退出,然后在浏览器里浏览http://localhost:8080 ,没错,一个自带webserver容器的web应用就这样跑起来了。

 

三、gradle 项目

3.1 build.gradle

buildscript {
	ext {
		springBootVersion = '1.5.2.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
	baseName = 'spring-boot-web-demo'
	version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
	mavenCentral()
}


dependencies {
	compile('org.springframework.boot:spring-boot-starter-web')
	compileOnly('org.projectlombok:lombok')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

3.2 项目结构

点击查看原图

3.3 配置文件application.yml

1 server:
2   port: 9090 #服务器端口
3   context-path: "/jimmy" #context-path
4 spring:
5   main:
6     banner-mode: "off" #启动时是否在控制台/日志里输出Spring字样Banner

spring-boot推荐配置使用新的yaml格式,更多默认的配置项请见参考文档2

3.4 运行及打包

spring-boot插件为gradle新增了2个task:bootRun、bootRepackage

分别用于运行及打包

gradle bootRun 、gradle bootRepackage 大家试下即可。打包成功后,/build/libs 下将生成可执行的jar包,复制到服务器上,java -jar spring-boot-web-demo-0.0.1-SNAPSHOT.jar 完事

 

参考文档:

1、http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html

2、http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值