日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Kotlin+SpringBoot服務端開發(fā)

Kotlin是什么?

創(chuàng)新互聯(lián)于2013年成立,先為五指山等服務建站,五指山等地企業(yè),進行企業(yè)商務咨詢服務。為五指山企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

著名IDE廠商JetBrains開發(fā)的基于JVM的靜態(tài)類型編程語言,聲稱100% interoperable with Java。Kotlin是由工程師設計的,各種細節(jié)設計非常切合工程師的需要。語法近似Java和Scala,且已活躍在Android開發(fā)領域,被譽為Android平臺的Swift。

Kotlin能與Java混合使用,并且直接復用Java的生態(tài)系統(tǒng)(庫、框架、工具)。一個已有的Java項目,只需引用Kotlin的Maven/Gradle插件,以及引用Kotlin標準庫的依賴,就可以逐漸摻入Kotlin代碼。你完全可以當它是a better Java。

Kotlin的學習曲線極其平緩,學習量相當于一個框架。有經(jīng)驗的程序員閱讀了文檔就能立刻用起來了。不信你看:

  • 原版文檔 http://kotlinlang.org/docs/re...
  • 中文文檔 http://kotlindoc.com

舉幾個例子來說明Kotlin的優(yōu)點吧,上代碼:

 
 
  1. //句尾不用寫分號
  2. // 自動推導變量類型,無需聲明
  3. val a = "Hello"
  4. // 簡單的println
  5. println(a.length() == 5)
  6. // 不用寫new, 直接調(diào)構(gòu)造函數(shù)
  7. val b = String("Hello")
  8. // 字符串插值
  9. "$a $b" == "Hello Hello"
  10. // if-else是表達式, 真方便!
  11. // ==相當于equals, 再也不怕忘寫equals了!
  12. val oneOrTwo = if (a == "Hello") 1 else 2
  13. // ===相當于Java的==
  14. (a === b) == false
  15. // Lambda用{}包起來,若有唯一參數(shù),參數(shù)名默認為it
  16. // 集合的函數(shù)式操作, 無需Java 8繁瑣的stream.collect(Collectors.toList())
  17. listOf(-1, 0, 1).map{it + 1}.filter{it > 0}) == listOf(1, 2)
  18. // 用一個默認值給null兜底
  19. val number = getNumberOrNull() ?: 0
  20. // 自動關閉的資源
  21. FileInputStream("MyFile").use { stream -> // 可指定參數(shù)名為stream, 取代默認的it
  22.   val firstByte = stream.read()
  23. }
  24. // 可以更簡單,一行
  25. val fileContent = File("MyFile").readText()
  26. // lazy, 延遲初始化
  27. class CPU {
  28.   val cpuCores by lazy { Runtime.getRuntime().availableProcessors() }

Kotlin為厭煩Java而疑慮Scala的人提供了避風港,為喜歡Groovy而想要靜態(tài)類型的人提供了避風港。啊!生活。

Spring Boot是什么?

Spring Boot是流行的Web快速開發(fā)框架,使基于Spring的開發(fā)更便捷。我們已經(jīng)知道Spring很好用,而Spring Boot的設計目標是:

  • 為一切Spring開發(fā)提供極速、通用的上手體驗
  • 開箱即用,但是當默認值不適合需求時不會妨礙你做改變
  • 提供一組適用于各種項目類型的非功能性特性(如內(nèi)嵌服務器、安全、度量、健康檢查、外部配置)
  • 完全不需要代碼生成和XML配置

Kotlin + Spring Boot

Kotlin能輕松集成Spring Boot,用Java怎么寫,用Kotlin基本上也怎么寫。

Spring能在線生成項目,免去創(chuàng)建項目的煩惱,請猛擊鏈接http://start.spring.io/ 。

我們用Gradle構(gòu)建,寫一個build.gradle文件:

 
 
  1. buildscript {
  2.   ext {
  3.     springBootVersion = '1.3.5.RELEASE'
  4.     kotlinVersion = '1.0.4'
  5.   }
  6.   repositories {
  7.     mavenCentral()
  8.   }
  9.   dependencies {
  10.     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  11.     classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
  12.   }
  13. }
  14. apply plugin: 'kotlin'
  15. apply plugin: 'spring-boot'
  16. jar {
  17.   baseName = 'myapp'
  18.   version = '0.1-SNAPSHOT'
  19. }
  20. sourceCompatibility = 1.8
  21. targetCompatibility = 1.8
  22. // class文件保留參數(shù)名稱
  23. compileJava.options.compilerArgs.add '-parameters'
  24. compileTestJava.options.compilerArgs.add '-parameters'
  25. springBoot {
  26.   mainClass = 'myapp.ApplicationKt'
  27. }
  28. dependencies {
  29.   compile 'org.springframework.boot:spring-boot-starter-aop'
  30.   compile 'org.springframework.boot:spring-boot-starter-web'
  31.   compile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}"

先寫一個主類Application.kt,放在src/main/kotlin目錄下(自己想一個包名哈),來啟動整個應用:

 
 
  1. @SpringBootApplication
  2. open class Application {
  3.   @Bean
  4.   open fun json(): MappingJackson2JsonView {
  5.     return MappingJackson2JsonView(ObjectMapper())
  6.   }
  7. }
  8. fun main(args: Array) {
  9.   SpringApplication.run(Application::class.java, *args)

Kotlin的函數(shù)可定義在類外面,而特殊的main函數(shù)要么放在外面,要么放在伴生對象(companion object)里面。這里就放在外面吧!

你會發(fā)現(xiàn)class和fun前面有open修飾符,它的意思是非final,Kotlin默認一切都是final的,如果不想要final救要加上open。由于Spring有時要創(chuàng)建代理,要求類和方法不能為final,因此我們每一處都寫上open,以免忘記。

這里只有一個json()方法,用來在Spring中初始化Jackson,這樣我們就能使用JSON了。

現(xiàn)在來寫一個RestController,提供RESTful API吧:

 
 
  1. @RestController
  2. @RequestMapping("/api/users")
  3. open class UserApi {
  4.     @RequestMapping("/{id}",  method = arrayOf(RequestMethod.GET))
  5.       open fun get(@PathVariable id: Long) = "User(id=$id, name=admin, password=123)"

好簡單啊!現(xiàn)在,在IDE中運行Application.kt文件,就開始運行了!用瀏覽器打開http://localhost:8080/api/use...

現(xiàn)在要把數(shù)據(jù)保存到數(shù)據(jù)庫了:

Spring Boot使用JPA非常簡單(照著官網(wǎng)的getting started學吧),但我要介紹另一種ORM框架——Ebean,它模仿了Rails的Active Record,支持常用的JPA注解。值得一提的是,Ebean的作者也喜歡Kotlin。

需要一個配置文件src/main/resources/ebean.properties :

 
 
  1. # 是否生成建表SQL
  2. ebean.db.ddl.generate=true
  3. # 是否執(zhí)行建表SQL
  4. ebean.db.ddl.run=false
  5. datasource.db.username=DB用戶名
  6. datasource.db.password=DB密碼
  7. datasource.db.databaseUrl=jdbc:mysql://localhost:3306/你的database名稱
  8. datasource.db.databaseDriver=com.mysql.jdbc.Driver 

我們對ebean.db.ddl.run(是否執(zhí)行建表SQL)選擇了false。因為Ebean會生成建表SQL,我們可以手動執(zhí)行,避免每次都重新建表,把數(shù)據(jù)丟棄了。編寫實體類后再運行,SQL會生成在項目目錄下,手動執(zhí)行一下吧!(亦可在***啟動前把ebean.db.ddl.run改成true)

然后在Spring中初始化Ebean吧:

 
 
  1. // 把這個方法添加到Application類
  2.   @Bean(autowire = Autowire.BY_TYPE)
  3.   open fun getEbeanServer(): EbeanServer {
  4.     val config = ServerConfig()
  5.     config.name = "db"
  6.     config.loadFromProperties()
  7.     config.isDefaultServer = true
  8.     return EbeanServerFactory.create(config)
  9.   } 

然后要修改main方法,在Spring之前先執(zhí)行Ebean的agent,改寫實體類的字節(jié)碼:

 
 
  1. fun main(args: Array) {
  2.   val packageName = "com.iostate.**" // 改成你自己的包名,實體類要放在這個包里面
  3.   if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent",
  4.                                           "debug=1;packages=$packageName")) {
  5.     System.err.println(
  6.       "avaje-ebeanorm-agent not found in classpath - not dynamically loaded")
  7.   }
  8.   SpringApplication.run(Application::class.java, *args)

Ebean需要執(zhí)行agent來改寫字節(jié)碼(instrumenation),而Hibernate則選擇了給實體對象創(chuàng)建動態(tài)代理(dynamic proxy),都是為了能對實體進行AOP操作。

instrumenation使用復雜,調(diào)試簡單;dynamic proxy使用簡單,調(diào)試復雜。各有千秋,我更認同改寫字節(jié)碼。

編寫實體類:

 
 
  1. import javax.persistence.*
  2. import com.avaje.ebean.Model
  3. import com.avaje.ebean.annotation.WhenCreated
  4. import com.avaje.ebean.annotation.WhenModified
  5. import java.sql.Timestamp
  6. import com.avaje.ebean.annotation.SoftDelete
  7. import com.fasterxml.jackson.annotation.JsonIgnore
  8. @MappedSuperclass
  9. abstract class BaseModel : Model() {
  10.   @Id @GeneratedValue
  11.   var id: Long = 0
  12.   @Version
  13.   var version: Long = 0
  14.   @WhenCreated
  15.   var whenCreated: Timestamp? = null
  16.   @WhenModified
  17.   var whenModified: Timestamp? = null
  18. }
  19. @Entity
  20. class User (
  21.     var name: String = "",
  22.     @JsonIgnore
  23.     var password: String = ""
  24.     @SoftDelete
  25.     var deleted: Boolean = false
  26. ) : BaseModel() {
  27.   companion object find : Find()

***個類是所有實體模型的基類,提供一些通用字段。id是自增主鍵,version是樂觀鎖的標志,whenCreated是創(chuàng)建時間,whenModified是修改時間。有的變量類型以問號結(jié)尾,這個跟Swift語言是一樣的,表示可為null(默認是非null的)。

第二類是User,行數(shù)很少,沒有繁瑣的getter/setter。@JsonIgnore的作用是防止敏感字段被泄露到JSON中,@SoftDelete的作用是軟刪除(數(shù)據(jù)不可見,但沒有真的刪除)。companion object find : Find()提供了一組快捷查詢方法,如byId(id)all() 。

現(xiàn)在把UserApi修改如下:

 
 
  1. @RestController
  2. @RequestMapping("/api/users")
  3. open class UserApi {
  4.   @RequestMapping("/{id}",  method = arrayOf(RequestMethod.GET))
  5.   open fun get(@PathVariable id: Long) = User.byId(id)
  6.   @RequestMapping("/new", method = arrayOf(RequestMethod.POST))
  7.   open fun create(@RequestParam name: String, @RequestParam password: String): User {
  8.     return User(name, password).apply {
  9.       save()
  10.     }
  11.   }

get方法真正向數(shù)據(jù)庫做查詢了!增加了create方法來創(chuàng)建用戶!如果想用瀏覽器快速測試,把RequestMethod.POST改成GET,輸入鏈接http://localhost:8080/api/use... 試試!

一個注意事項

Spring Boot能把程序打包成jar直接運行,這是很方便群眾的!但是JSP和Ebean在jar模式都無法工作。

那么在生產(chǎn)環(huán)境要怎么解決呢?可以把jar解壓運行!

參考文檔的exploded archives: http://docs.spring.io/spring-...

 
 
  1. # 解壓
  2. unzip -q myapp.jar
  3. # 運行
  4. java org.springframework.boot.loader.JarLauncher
  5. # 生產(chǎn)模式用以下的nohup方式,以防程序隨著shell一起關閉
  6. nohup java org.springframework.boot.loader.JarLauncher & 

我自己用的命令不一樣:

 
 
  1. unzip -q myapp.jar
  2. nohup java -cp '.:./lib/*' com.myapp.ApplicationKt & 

注意當前所在的工作目錄,日志目錄/logs會創(chuàng)建在當前工作目錄下。

收工

我提供了一個示例項目,比較粗糙,請多多包涵 https://github.com/sorra/bms

老外也有幾個示例項目,可供參考:

Spring Boot Kotlin project with a REST Webservice and Spring Data: https://github.com/sdeleuze/s...

Demo Webapp using SpringBoot, Kotlin and React.js: https://github.com/winterbe/s...

順帶一提,輕境界就是用Kotlin + Spring Boot構(gòu)建的!


當前文章:Kotlin+SpringBoot服務端開發(fā)
分享地址:http://www.dlmjj.cn/article/dpiphgi.html