spring-boot 速成(1) helloworld

一、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

 

posted @ 2017-03-15 17:15  菩提树下的杨过  阅读(2948)  评论(0编辑  收藏  举报