您的位置:澳门新葡8455最新网站 > 编程教学 > 转业于构建最精简的Android项目

转业于构建最精简的Android项目

发布时间:2019-10-05 16:06编辑:编程教学浏览(188)

    本章介绍Spring Boot集成Kotlin混合Java开采一个整机的spring boot应用:Restfeel,三个商家级的Rest API接口测量检验平台(在开源工程restfiddle[1]基本功上开辟而来)。

    github:ONE-Kotlin

    编制程序语言:Java,Kotlin

    什么是kotlin?

    数据库:Mongo

    Kotlin 是一个基于 JVM 的新的编制程序语言,由 JetBrains 开采。 Kotlin可以编写翻译成Java字节码,也能够编写翻译成JavaScript,方便在平素不JVM的设施上运营。 JetBrains,作为当前广受招待的Java IDE AMDliJ 的提供商,在 Apache 许可下已经开源其Kotlin 编制程序语言。

    Spring框架:Spring data jpa,Spring data mongodb

    kotlin有何样好处?

    前端:jquery,requireJS,

    兼容Java的语言。

    工程创设筑工程具:Gradle

    让它比Java更安全,可以静态检查实验常见的陷阱。如:引用空指针

    图片 1

    让它比Java更简单,通过支撑variable type inference,higher-order functions (closures),extension functions,mixins and first-class

    Kotlin是一种温婉的言语,是JetBrains公司开销的静态类型JVM语言,与Java无缝集成。与Java比较,Kotlin的语法更简洁、更具表明性,并且提供了更加多的风味,比方,高阶函数、操作符重载、字符串模板。它与Java高度可互操作,能够并且用在贰个种类中。这门语言的靶子是:

    delegation等实现。

    • 创办一种宽容Java的语言

    让它比最成熟的竞争对手Scala语言越发简便易行。

    • 编写翻译速度至少同Java同样快
    • 比Java更安全,能够静态检查实验常见的牢笼。如:引用空指针
    • 比Java更加精简,通过帮忙 variable type inference,higher-order functions ,extension functions,mixins and first-class delegation 等得以实现。
    • 比最成熟的竞争者Scala还简要

    重组ANKO轻便落成Android-MVVM形式。

    Kotlin不像Scala另起炉灶,将类库,特别是群集类都本人来了一回。kotlin是对现存java的滋长,通过扩展方法给java提供了重重诸如fp之类的特征,但与此同一时候从来维持对java的合营。那也是是kotlin官方网站首页重视重申的:

    看代码在此以前先来几张效果图吧

    100% interoperable with Java™。

    图片 2

    Kotlin和Scala很像,对于用惯了Scala的人的话用起来很顺手,对于爱好函数式的开荒者,Kotlin是个不错的挑三拣四。有个小点,像Groovy动态类型就无须说了,Scala函数最终返回值都足以省去return,不过不明了为什么Kotlin对return情有独寄。

    图片 3

    其它,Kotlin可以编写翻译成Java字节码,也得以编写翻译成JavaScript,在尚未JVM的条件运营。

    图片 4

    Kotlin创造类的艺术与Java类似,例如上面的代码成立了三个有八个属性的Person类:

    图片 5

    class Person{ var name: String = "" var age: Int = 0 var sex: String? = null}
    

    图片 6

    能够观望,Kotlin的变量评释格局略有一些差别。在Kotline中,证明变量必得运用首要字var,而一旦要成立三个只读/只赋值一遍的变量,则必要采纳val取代它。

    图片 7

    Kotlin对函数式编制程序的完成适度

    看完效果图,你能够考虑下那么些功效用java去写,你会怎么去设计,又会敲多少行代码呢?

    三个函数指针的例子:

    用完kotlin你就能够明白,原本代码能够这么写,三个原生Android项目代码原本能够那样轻便

    /** * "Callable References" or "Feature Literals", i.e. an ability to pass * named functions or properties as values. Users often ask * "I have a foo() function, how do I pass it as an argument?". * The answer is: "you prefix it with a `::`". */fun main(args: Array<String>) { val numbers = listOf println(numbers.filter}fun isOdd = x % 2 != 0
    

    和自家一块来创制最精简的Android项目吧

    运作结果: [1, 3]

    品种并入了环信即时通信成效,提供五个测验账号账号A:111,账号B:222[密码与帐号一样]

    再看四个复合函数的例证。看了上边的复合函数的事例,你会发觉Kotlin的FP的兑现卓殊轻松。(跟纯数学的表明式,非常邻近了)

    有如何不懂的能够找小编,共同研究 QQ群:581621024

    /** * The composition function return a composition of two functions passed to it: * compose = f. * Now, you can apply it to callable references. */fun main(args: Array<String>) { val oddLength = compose(::isOdd, ::length) val strings = listOf("a", "ab", "abc") println(strings.filter(oddLength))}fun isOdd = x % 2 != 0fun length(s: String) = s.lengthfun <A, B, C> compose -> C, g:  -> B):  -> C { return { x -> f }}
    

    运作结果: [a,abc]

    大约表达下
    val oddLength = compose(::isOdd, ::length) val strings = listOf("a", "ab", "abc") println(strings.filter(oddLength))
    

    那正是数学中,复合函数的定义:

    h = hg: A->Bf: B->Ch: A->Cg = f = f = C
    

    只是代码中的写法是:

    h=composeh=compose, g
    

    有关Kotlin,官方网址有三个百般好的交互式Kotlin学习课程:

    想深切语言完毕细节的,可以直接参谋github源码[3]。

    1.build.gradle中增添kotlin相关注重

    运用插件

    apply { plugin "kotlin" plugin "kotlin-spring" plugin "kotlin-jpa" plugin "org.springframework.boot" plugin 'java' plugin 'eclipse' plugin 'idea' plugin 'war' plugin 'maven'}
    

    创设时插件信赖

    buildscript { ext { kotlinVersion = '1.1.0' springBootVersion = '1.5.2.RELEASE' } dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion" classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion" } repositories { mavenLocal() mavenCentral() maven { url "http://oss.jfrog.org/artifactory/oss-release-local" } maven { url "http://jaspersoft.artifactoryonline.com/jaspersoft/jaspersoft-repo/" } maven { url "https://oss.sonatype.org/content/repositories/snapshots" } }}
    

    编译时jar包依赖

    dependencies { compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion") compile("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion") compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.4") ...}
    
    2.配置源码目录sourceSets
    sourceSets { main { kotlin { srcDir "src/main/kotlin" } java { srcDir "src/main/java" } } test { kotlin { srcDir "src/test/kotlin" } java { srcDir "src/test/java" } }}```指定kotlin,java源码放置目录。让java的归java,Kotlin的归Kotlin。这样区分开来。这个跟scala的插件实现方式上有点区别。scala的插件,是允许你把scala,java代码随便放,插件会自动寻找scala代码编译。整个工程大目录如下图所示:![](http://upload-images.jianshu.io/upload_images/1233356-20285f52eb50f486.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)#####3.写Kotlin代码Entity实体类```package com.restfeel.entityimport org.bson.types.ObjectIdimport org.springframework.data.mongodb.core.mapping.Documentimport java.util.*import javax.persistence.GeneratedValueimport javax.persistence.GenerationTypeimport javax.persistence.Idimport javax.persistence.Version@Document(collection = "blog") // 如果不指定collection,默认遵从命名规则class Blog { @Id @GeneratedValue(strategy = GenerationType.AUTO) var id: String = ObjectId.get().toString() @Version var version: Long = 0 var title: String = "" var content: String = "" var author: String = "" var gmtCreated: Date = Date() var gmtModified: Date = Date() var isDeleted: Int = 0 //1 Yes 0 No var deletedDate: Date = Date() override fun toString(): String { return "Blog(id='$id', version=$version, title='$title', content='$content', author='$author', gmtCreated=$gmtCreated, gmtModified=$gmtModified, isDeleted=$isDeleted, deletedDate=$deletedDate)" }}```Service类```package com.restfeel.serviceimport com.restfeel.entity.Blogimport org.springframework.data.mongodb.repository.MongoRepositoryimport org.springframework.data.mongodb.repository.Queryimport org.springframework.data.repository.query.Paraminterface BlogService : MongoRepository<Blog, String> { @Query("{ 'title' : ?0 }") fun findByTitle(@Param title: String): Iterable<Blog>}```Controller类```package com.restfeel.controllerimport com.restfeel.entity.Blogimport com.restfeel.service.BlogServiceimport org.springframework.boot.autoconfigure.EnableAutoConfigurationimport org.springframework.context.annotation.ComponentScanimport org.springframework.security.core.context.SecurityContextHolderimport org.springframework.security.core.userdetails.UserDetailsimport org.springframework.stereotype.Controllerimport org.springframework.transaction.annotation.Propagationimport org.springframework.transaction.annotation.Transactionalimport org.springframework.ui.Modelimport org.springframework.web.bind.annotation.GetMappingimport org.springframework.web.bind.annotation.PostMappingimport org.springframework.web.bind.annotation.RequestParamimport org.springframework.web.bind.annotation.ResponseBodyimport java.util.*import javax.servlet.http.HttpServletRequest/** * 文章列表,写文章的Controller * @author Jason Chen 2017/3/31 01:10:16 */@Controller@EnableAutoConfiguration@ComponentScan@Transactional(propagation = Propagation.REQUIRES_NEW)class BlogController(val blogService: BlogService) { @GetMapping("/blogs.do") fun listAll(model: Model): String { val authentication = SecurityContextHolder.getContext().authentication model.addAttribute("currentUser", if (authentication == null) null else authentication.principal as UserDetails) val allblogs = blogService.findAll() model.addAttribute("blogs", allblogs) return "jsp/blog/list" } @PostMapping("/saveBlog") @ResponseBody fun saveBlog(blog: Blog, request: HttpServletRequest):Blog { blog.author = (request.getSession().getAttribute("currentUser") as UserDetails).username return blogService.save } @GetMapping("/goEditBlog") fun goEditBlog(@RequestParam(value = "id") id: String, model: Model): String { model.addAttribute("blog", blogService.findOne return "jsp/blog/edit" } @PostMapping("/editBlog") @ResponseBody fun editBlog(blog: Blog, request: HttpServletRequest) :Blog{ blog.author = (request.getSession().getAttribute("currentUser") as UserDetails).username blog.gmtModified = Date() blog.version = blog.version + 1 return blogService.save } @GetMapping fun blogDetail(@RequestParam(value = "id") id: String, model: Model): String { model.addAttribute("blog", blogService.findOne return "jsp/blog/detail" } @GetMapping("/listblogs") @ResponseBody fun listblogs(model: Model) = blogService.findAll() @GetMapping("/findBlogByTitle") @ResponseBody fun findBlogByTitle(@RequestParam(value = "title") title: String) = blogService.findByTitle}```对应的前端代码,这里就不多说了。可以参考工程源代码[4]。SpringBoot的启动类: 我们用Kotlin写SpringBoot的启动类:```package com.restfeelimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.boot.CommandLineRunnerimport org.springframework.boot.SpringApplicationimport org.springframework.core.env.Environment/** * Created by jack on 2017/3/29. * @author jack * @date 2017/03/29 */@RestFeelBootclass RestFeelApplicationKotlin : CommandLineRunner { @Autowired private val env: Environment? = null override fun run(vararg args: String?) { println("RESTFEEL 启动完毕") println("应用地址:" + env?.getProperty("application.host-uri")) }}fun main(args: Array<String>) { SpringApplication.run(RestFeelApplicationKotlin::class.java, *args)}```其中,@RestFeelBoot是自定义注解,代码如下:```package com.restfeel;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Created by jack on 2017/3/23. * * @author jack * @date 2017/03/23 */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@Configuration@EnableAutoConfiguration@ComponentScanpublic @interface RestFeelBoot { Class<?>[] exclude() default {};}```#####运行测试命令行输入gradle bootRun,运行应用。 访问 http://127.0.0.1:5678/blogs.do 文章列表![](http://upload-images.jianshu.io/upload_images/1233356-365b79713f187cc3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)写文章![](http://upload-images.jianshu.io/upload_images/1233356-87929f0b83a43ad8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)阅读文章![](http://upload-images.jianshu.io/upload_images/1233356-4b12e505588c6b3f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)##小结本章示例工程源代码:https://github.com/Jason-Chen-2017/restfeel/tree/restfeel_kotlin_2017.5.6参考资料:1.https://github.com/AnujaK/restfiddle2.http://www.jianshu.com/c/498ebcfd27ad3.https://github.com/JetBrains/kotlin4.https://github.com/Jason-Chen-2017/restfeel/tree/restfeel_kotlin_2017.5.6
    

    本文由澳门新葡8455最新网站发布于编程教学,转载请注明出处:转业于构建最精简的Android项目

    关键词: