|
| 1 | +###SpringBoot Scala |
| 2 | + |
| 3 | +可以说近几年Spark的流行带动了Scala的发展,它集成了面向对象编程和函数式编程的各种特性,Scala具有更纯Lambda表粹的函数式业务逻辑解决方案,其语法比Java8后Lambda更加简洁方便,SpringBoot为Spring提供了一种更加方便快捷的方式,不再要求写大量的配置文件,作为一名Scala爱好者,使用SpringBoot结合Scala将大大节省我们开发的时间以及代码量 |
| 4 | + |
| 5 | +`build.gradle` |
| 6 | + |
| 7 | + buildscript { |
| 8 | + ext { |
| 9 | + springBootVersion = '1.4.3.RELEASE' |
| 10 | + } |
| 11 | + repositories { |
| 12 | + mavenCentral() |
| 13 | + } |
| 14 | + dependencies { |
| 15 | + classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") |
| 16 | + } |
| 17 | + } |
| 18 | + apply plugin: 'java' |
| 19 | + apply plugin: 'scala' |
| 20 | + apply plugin: 'eclipse' |
| 21 | + apply plugin: 'application' |
| 22 | + apply plugin: 'org.springframework.boot' |
| 23 | + |
| 24 | + ext { |
| 25 | + sourceCompatibility = 1.8 |
| 26 | + targetCompatibility = 1.8 |
| 27 | + } |
| 28 | + jar { |
| 29 | + baseName = 'spring-boot-scala' |
| 30 | + version = '0.1.0' |
| 31 | + } |
| 32 | + |
| 33 | + repositories { |
| 34 | + mavenCentral() |
| 35 | + maven { url "https://repo.spring.io/snapshot" } |
| 36 | + maven { url "https://repo.spring.io/milestone" } |
| 37 | + } |
| 38 | + |
| 39 | + dependencies { |
| 40 | + compile("org.springframework.boot:spring-boot-starter-web") |
| 41 | + compile("org.springframework.boot:spring-boot-starter-actuator") |
| 42 | + compile("org.thymeleaf:thymeleaf-spring4") |
| 43 | + compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect") |
| 44 | + compile "org.scala-lang:scala-library:2.11.7" |
| 45 | + compile "org.scala-lang:scala-compiler:2.11.7" |
| 46 | + compile "org.scala-lang:scala-reflect:2.11.7" |
| 47 | + testCompile("org.springframework.boot:spring-boot-starter-test") |
| 48 | + testCompile("junit:junit") |
| 49 | + } |
| 50 | + |
| 51 | + task wrapper(type: Wrapper) { |
| 52 | + gradleVersion = '2.9' |
| 53 | + } |
| 54 | + |
| 55 | + bootRepackage.enabled = false |
| 56 | + |
| 57 | +```Scala |
| 58 | + |
| 59 | +package com.silence |
| 60 | + |
| 61 | +import org.springframework.context.annotation.Configuration |
| 62 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration |
| 63 | +import org.springframework.context.annotation.ComponentScan |
| 64 | +import org.springframework.boot.SpringApplication |
| 65 | +import org.springframework.boot.autoconfigure.SpringBootApplication |
| 66 | + |
| 67 | +@Configuration |
| 68 | +@EnableAutoConfiguration |
| 69 | +@ComponentScan |
| 70 | +@SpringBootApplication |
| 71 | +class Config |
| 72 | + |
| 73 | + |
| 74 | +object SpringBootScalaApplication extends App { |
| 75 | + |
| 76 | + SpringApplication.run(classOf[Config]) |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +``` |
| 82 | + |
| 83 | +```Scala |
| 84 | + |
| 85 | +package com.silence.controller |
| 86 | + |
| 87 | +import org.springframework.stereotype.Controller |
| 88 | +import org.springframework.web.bind.annotation.RequestMapping |
| 89 | +import org.springframework.web.bind.annotation.RequestMethod |
| 90 | +import org.springframework.context.annotation.ComponentScan |
| 91 | +import org.springframework.context.annotation.Configuration |
| 92 | +import org.springframework.web.bind.annotation.ResponseBody |
| 93 | + |
| 94 | +@ComponentScan |
| 95 | +@Controller |
| 96 | +class UserController { |
| 97 | + |
| 98 | + @ResponseBody |
| 99 | + @RequestMapping(value = Array("/index"), method = Array(RequestMethod.GET)) |
| 100 | + def list(str : String) = { |
| 101 | + print("hello world") |
| 102 | + "index" |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +``` |
| 108 | + |
| 109 | +###build |
| 110 | + |
| 111 | + gradle bootRun |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +项目地址[github.com/silence940109/SpringBoot-Scala](github.com/silence940109/SpringBoot-Scala) |
0 commit comments