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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM