springboot入門(一)

我個(gè)人將互聯(lián)網(wǎng)這十年的發(fā)展定義為技術(shù)的第二次革命,從單機(jī)服務(wù)C/S再到SOA分布式威彰,以至于現(xiàn)在的PAAS出牧,不斷的向更高級(jí),更加智能進(jìn)行演化歇盼,基礎(chǔ)的平臺(tái)舔痕,組件,中間件,也趨于云化伯复,整個(gè)互聯(lián)網(wǎng)的技術(shù)環(huán)境也趨向于從研發(fā)技術(shù)向使用技術(shù)的階段慨代,開源與共享讓每個(gè)team,可以在最短的時(shí)間內(nèi)具有即使通訊能力啸如,推送能力侍匙,短信能力,人臉識(shí)別能力叮雳,自然語音識(shí)別能力想暗。那么對(duì)于springboot,將更加簡化之前的spring mvc+tomcat+xml等組合復(fù)雜度帘不,只需引入starter即可完成web服務(wù)的搭建啟用说莫,對(duì)于restful更加的靈活快速。

那么對(duì)于程序員寞焙,最好能夠使用gradle來進(jìn)行項(xiàng)目與jar包的管理储狭,無需一堆xml文件,gradle基于groovy語言的捣郊。以下為springboot需要的gradle的配置辽狈。

group'com.terry'

version'1.0-SNAPSHOT'

applyplugin:'java'

applyplugin:'spring-boot'

sourceCompatibility =1.5

//buildscript 是項(xiàng)目本身所需要的資源

buildscript {

ext {

springBootVersion ='1.4.0.RELEASE'

}

repositories {

mavenCentral()

}

dependencies {

classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

}

}

repositories {

repositories { maven { url'http://repo.qtonecloud.cn/content/groups/public/'} }

mavenLocal()

mavenCentral()

}

/**

* The imported bom will control the version of the dependency.

It will also control the version of its transitive dependencies if they’re listed in the bom.

*/

//dependencyManagement {

//? ? imports {

//? ? ? ? mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2'

//? ? }

//}

dependencies {

compile("org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE")

compile'org.springframework.cloud:spring-cloud-starter-config:1.2.1.RELEASE'

compile'org.aspectj:aspectjweaver:1.8.8'

compile'com.google.guava:guava:19.0'

testCompilegroup:'junit',name:'junit',version:'4.11'

}

task"createJavaProject"<< {

sourceSets*.java.srcDirs*.each { it.mkdirs() }

sourceSets*.resources.srcDirs*.each { it.mkdirs() }

/** 可選生成子文件夾 */

//? ? file("src/main/filters").mkdirs()

//? ? file("src/main/assembly").mkdirs()

//? ? file("src/main/config").mkdirs()

//? ? file("src/main/scripts").mkdirs()

/** 填充文件,便于git提交 */

file("src/main/java/cn/terry").mkdirs()

file("src/main/java/cn/terry/package-info.java").createNewFile()

file("src/main/resources").mkdirs()

file("src/main/resources/spring.xml").createNewFile()

file("src/test/java/cn/terry").mkdirs()

file("src/test/java/cn/terry/package-info.java").createNewFile()

file("src/test/resources/spring.xml").createNewFile()

}

創(chuàng)建服務(wù)啟動(dòng)主程序

packagecn.terry;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;

importorg.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;

importorg.springframework.boot.web.support.SpringBootServletInitializer;

/**

* Created by zhaoguofeng on 2016/11/22.

*/

@SpringBootApplication

public classStartApplication extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {

public static voidmain(String[] args) {

SpringApplication.run(StartApplication.class,args);

}

public void customize(ConfigurableEmbeddedServletContainer container) {

container.setPort(8082);

}

}

端口當(dāng)然可以通過實(shí)現(xiàn)EmbeddedServletContainerCustomizer.customize方法呛牲,去進(jìn)行設(shè)置刮萌。

創(chuàng)建一個(gè)controller

packagecn.terry.controller;

importcom.google.common.collect.Lists;

importorg.springframework.context.annotation.ComponentScan;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RestController;

importjava.util.List;

/**

* Created by zhaoguofeng on 2016/11/22.

*/

@RestController

@ComponentScan

public classLoginController {

@RequestMapping("/getInfo")

publicListgetInfo() {

List list = Lists.newArrayList();

list.add("hello");

list.add("world");

returnlist;

}

}

執(zhí)行main方法,我們可以看到spring boot就很輕松的啟動(dòng)起來了侈净,那么我們運(yùn)行http://localhost:8082/getInfo? 即可看到["hello","world"]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末尊勿,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子畜侦,更是在濱河造成了極大的恐慌元扔,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旋膳,死亡現(xiàn)場離奇詭異澎语,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)验懊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門擅羞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人义图,你說我怎么就攤上這事减俏。” “怎么了碱工?”我有些...
    開封第一講書人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵娃承,是天一觀的道長奏夫。 經(jīng)常有香客問我,道長历筝,這世上最難降的妖魔是什么酗昼? 我笑而不...
    開封第一講書人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮梳猪,結(jié)果婚禮上麻削,老公的妹妹穿的比我還像新娘。我一直安慰自己春弥,他們只是感情好呛哟,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著惕稻,像睡著了一般竖共。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上俺祠,一...
    開封第一講書人閱讀 51,692評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音借帘,去河邊找鬼蜘渣。 笑死,一個(gè)胖子當(dāng)著我的面吹牛肺然,可吹牛的內(nèi)容都是我干的蔫缸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了调鲸?” 一聲冷哼從身側(cè)響起姓惑,我...
    開封第一講書人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎桩盲,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡防症,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了哎甲。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蔫敲。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖炭玫,靈堂內(nèi)的尸體忽然破棺而出奈嘿,到底是詐尸還是另有隱情,我是刑警寧澤吞加,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布裙犹,位于F島的核電站尽狠,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏伯诬。R本人自食惡果不足惜晚唇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盗似。 院中可真熱鬧哩陕,春花似錦、人聲如沸赫舒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽接癌。三九已至心赶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間缺猛,已是汗流浹背缨叫。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留荔燎,地道東北人耻姥。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像有咨,于是被迫代替她去往敵國和親琐簇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容