我個(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"]