自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Kotlin + Spring Boot服務端開發(fā)

開發(fā) 后端
Kotlin能與Java混合使用,并且直接復用Java的生態(tài)系統(tǒng)(庫、框架、工具)。一個已有的Java項目,只需引用Kotlin的Maven/Gradle插件,以及引用Kotlin標準庫的依賴,就可以逐漸摻入Kotlin代碼。你完全可以當它是a better Java。Spring Boot是流行的Web快速開發(fā)框架,使基于Spring的開發(fā)更便捷。

[[175220]]

Kotlin是什么?

著名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)驗的程序員閱讀了文檔就能立刻用起來了。不信你看:

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

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

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.  
  17. jar { 
  18.   baseName = 'myapp' 
  19.   version = '0.1-SNAPSHOT' 
  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}" 
  32.  

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

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

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)" 
  6.  

好簡單啊!現(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.  
  6. datasource.db.username=DB用戶名 
  7. datasource.db.password=DB密碼 
  8. datasource.db.databaseUrl=jdbc:mysql://localhost:3306/你的database名稱 
  9. 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<String>) { 
  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) 
  9.  

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.  
  3. import com.avaje.ebean.Model 
  4. import com.avaje.ebean.annotation.WhenCreated 
  5. import com.avaje.ebean.annotation.WhenModified 
  6. import java.sql.Timestamp 
  7.  
  8. import com.avaje.ebean.annotation.SoftDelete 
  9. import com.fasterxml.jackson.annotation.JsonIgnore 
  10.  
  11. @MappedSuperclass 
  12. abstract class BaseModel : Model() { 
  13.  
  14.   @Id @GeneratedValue 
  15.   var id: Long = 0 
  16.  
  17.   @Version 
  18.   var version: Long = 0 
  19.  
  20.   @WhenCreated 
  21.   var whenCreated: Timestamp? = null 
  22.  
  23.   @WhenModified 
  24.   var whenModified: Timestamp? = null 
  25.  
  26.  
  27. @Entity 
  28. class User ( 
  29.     var name: String = ""
  30.     @JsonIgnore 
  31.     var password: String = "" 
  32.     @SoftDelete 
  33.     var deleted: Boolean = false 
  34. ) : BaseModel() { 
  35.   companion object find : Find<Long, User>() 
  36.  

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

第二類是User,行數(shù)很少,沒有繁瑣的getter/setter。@JsonIgnore的作用是防止敏感字段被泄露到JSON中,@SoftDelete的作用是軟刪除(數(shù)據(jù)不可見,但沒有真的刪除)。companion object find : Find<Long, User>()提供了一組快捷查詢方法,如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.  
  7.   @RequestMapping("/new", method = arrayOf(RequestMethod.POST)) 
  8.   open fun create(@RequestParam name: String, @RequestParam password: String): User { 
  9.     return User(namepassword).apply { 
  10.       save() 
  11.     } 
  12.   } 
  13.  

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)建的!

責任編輯:龐桂玉 來源: segmentfault
相關推薦

2013-03-25 10:08:44

PHPWeb

2016-08-04 14:41:21

架構(gòu)java服務端開發(fā)

2016-03-18 09:04:42

swift服務端

2012-03-02 10:38:33

MySQL

2010-08-03 09:59:30

NFS服務

2015-11-09 17:51:12

服務器端開發(fā)

2021-05-25 08:20:37

編程技能開發(fā)

2010-03-18 18:09:36

Java Socket

2021-04-26 13:20:06

Vue服務端渲染前端

2023-08-08 08:17:23

VasDolly服務端參數(shù)

2010-03-19 18:17:17

Java Server

2010-02-24 15:42:03

WCF服務端安全

2009-08-21 15:22:56

端口偵聽

2022-12-29 08:56:30

監(jiān)控服務平臺

2009-08-21 15:59:22

服務端與客戶端通信

2011-09-09 09:44:23

WCF

2009-08-21 16:14:52

服務端與客戶端通信

2018-05-04 15:27:22

Spring Boo Web開發(fā)

2022-05-18 08:32:05

服務監(jiān)控Prometheus開源

2024-03-06 14:58:52

客戶端微服務架構(gòu)
點贊
收藏

51CTO技術棧公眾號