春天 5.0 - 即使是鸽友
在本文中,我将展示Spring和彩票3d字谜如何一起使用。如果您不熟悉我最近的文章,请看看其他彩票3d字谜相关的帖子 这里 。除了kotlin,我一直有兴趣与之合作 春天 自从我在2011年开始java以来,我仍然喜欢这个框架,虽然它变得更大,更大,你往往不太了解所有替代方案中选择哪种功能。随着框架本身正在增长, 文件也是你能看到的最好的,也是如此。
我最喜欢春天的事情是,您可以从第一天开始关注您的业务逻辑,并没有在启动之前设置的技术,基础设施。春天是通过封装许多用于某些任务所必需的样板并提供我们可以应用的简单注释,以便利用这些功能。最着名的模块之一肯定是 春天web mvc.,这在JVM上涉及到Web服务时被广泛使用。
无功规划 - 非阻塞方式
你可能已经注意到了这一点 反应规划 最近得到更多关注。有许多框架新兴,想要鼓励这种规划风格,即 rxjava. , Vert.x. 或者 阿克斯 例如。如果你从未遇到过这些,你可以阅读我的帖子 kotlin与vert.x. as a first step.
弹簧反应
What does this have to do with Spring though? Well, of course, there’s yet another library for building reactive systems, which in fact is powered by Spring: [Project Reactor] //projectreactor.io). Reactor is used in the current Spring Release 5.0, available since September 2017, which introduces a 反应网络框架 叫 webflux .
这一事实是一个充分的理由让我潜入它,因为它听起来很棒,知道Web MVC已经作为Spring的优秀模块。但是,还有其他有原因要考虑到这一扩展:春天大大支持 kotlin. 甚至介绍了 kotlin. 专注 近期主要版本的功能 - 这是通过利用扩展函数来实现的,以便通过介绍彩票3d字谜 DSL,您可以在我的帖子中阅读的功能 使用彩票3d字谜创建DSL。其中一个新的DSL与春天携手共进 webflux :用于描述WebFlux备份Web服务的功能性DSL。事实上,这是我将在一个非常短的榜样中向您介绍...
Webflux和彩票3d字谜行动
让我们来看看一个非常基本的应用程序 春天webflux 在一个 kotlin. 应用。初始设置可以轻松下载为一个 Springboot. 应用 春天 initializr.,如果您选择彩票3d字谜作为编程语言,并且还可以启用“无功Web”依赖项,这是SpringBoot 2.0.0以来可用的。
As soon as we’ve imported this project into our IDE, we can start with creating a reactive web service. For the sake of brevity, I chose a very simple, not very useful, example: An internally managed repository of simple String
s that is populated through the web interface and also is searchable from it. Thanks to 彩票3d字谜 and also Spring, there's not much code that has to be written:
回购和处理程序
@Component
class ReactiveHandler(val repo: StringRepo) {
fun getText(search: String): Mono<String> =
repo.get(search).toMono().map { "Result: $it!" }
fun addText(text: String): Mono<String> =
repo.add(text).toMono().map { "Result: $it!" }
fun getAllTexts(): Flux<String> =
repo.getAll().toFlux().map { "Result: $it" }
}
@Component
class StringRepo {
private val entities = mutableListOf<String>()
fun add(s: String) = entities.add(s)
fun get(s: String) = entities.find { it == s } ?: "not found!"
fun getAll() = listOf(entities)
}
We simply create a repository that maintains a list of String
s and another class ReactiveHandler
, which is responsible for delegating to the repository and providing "reactive types" defined in Reactor. These are mandatory for WebFlux: Flux
and Mono
(Read about them 这里 ). Regardless of their intention, have a look at how they are created: toMono()
and toFlux()
are examples of extension functions added in Spring 5.0, a feature dedicated to 彩票3d字谜. The much more interesting part though is where the web routing is defined. This part in particular is where the already mentioned functional DSL comes into play. Let’s observe how it works.
功能性WebFlux DSL。
@Configuration
class RoutingConfiguration {
@Bean
fun routerFunction(handler: ReactiveHandler): RouterFunction<ServerResponse> = router {
("/reactive").nest {
val searchPathName = "search"
val savePathName = "save"
GET("/{$searchPathName}") { req ->
val pathVar = req.pathVariable(searchPathName)
ServerResponse.ok().body(
handler.getText(pathVar)
)
}
GET("/") {
ServerResponse.ok().body(handler.getAllTexts())
}
PUT("/{$savePathName}") { req ->
val pathVar = req.pathVariable(savePathName)
ServerResponse.ok().body(
handler.addText(pathVar)
)
}
}
}
}
The router
function is the entry point of the new DSL, which can be inspected on GitHub. 。由于DSL提供了更多方法,所示的解决方案只是您可以选择的更多方法。通过我的定义,服务器在“/反应”下启动Web服务,并接受两个 得到 和一个 放 request, each of which is delegated to the previously shown ReactiveHandler
(see method parameter) before the results are put into a ServerResponse
. Of course, you’d have to handle errors in a real-world scenario and "ok" wouldn’t be the only response.
益处
如果您向我询问,这种方法结构非常干净,甚至提供了使用任何彩票3d字谜代码来定义变量,循环,条件,在实际DSL代码内的任何kotlin代码的机会。鉴于此,您有一个非常强大的工具,可以以非常自然的程序方式使用。
如果您喜欢检查出来,则代码可用 GitHub. repository.
包装和透视
我提出了一个小型项目,该项目正在使用Spring 5.0及其新模块WebFlux与彩票3d字谜结合使用。我认为这一事实,春天正式使用和支持彩票3d字谜是一个 非常 重要的是,我想再次强调。
kotlin. - 这不仅是Android!
我们都知道彩票3d字谜进入Android,这是可能的,因为谷歌几个月前宣布了官方支持。在服务器端,虽然,人们,尤其是谈到彩票3d字谜的时候犹豫不决。他们倾向于怀疑彩票3d字谜是否真的成熟了。
当你 问我 ,没有充分的理由犹豫不决。许多项目已经使用彩票3d字谜,Frameworks支持彩票3d字谜,甚至使用专用彩票3d字谜功能扩展其库。春天,作为最常见的Java框架之一,似乎认为他们很快就像kotlin一样 选择 对于SpringBoot应用程序的Java和Groovy。最新的发展是春季5.0的一部分,是下一步,其中一些我们在这篇小文章中观察到。如果你和我一样,有兴趣传播彩票3d字谜作为Java的替代方案,谈论它并告诉你的同事了关于春天的支持和实际情况
happening 😉
特别感谢
正如您可以阅读的那样 文章 ,春天介绍了很多kotlin功能。有一个人, Sébastiendeleuze.,谁对春季框架的这种发展负责。他也是客人 谈谈kotlin.
已经。在彩票3d字谜社区拥有这样的影响力真的很棒,非常感谢! 保持伟大的工作。
如果您希望查看我的示例,则该代码可在此处提供:
Git. 。随意提供任何反馈,我总是很乐意提供帮助。此外,如果你愿意,请看看我的 推特 如果您对更多彩票3d字谜的东西感兴趣,请遵循账户,谢谢。
Simon是一名软件工程师,拥有9年以上的经验开发在多个平台上的软件,包括JVM和无服务环境。他目前为决策自动化SaaS平台构建可扩展的分布式服务。西蒙是一个自我任命的kotlin爱好者。
感谢贡献,西蒙!
One thing there –恕我直言,与常规非功能DSL 1相比,功能方法更加复杂。
是的,我知道你的意思,它’虽然,_one place_所有。使用注释的常规方法导致众多类的非常分布式配置可以包含此类配置相关的注释….
当然,条件说明就像使用普通源代码一样简单得多。
我完全同意Dusan。对于几个操作来说,它可能看起来很好,但我认为在具有更复杂的休息API的项目中,我认为它不会是可读的或容易的。
配置问题可以用适当的包装(六角形架构方法)处理。
I’m仍然想要实验,但我们使用SQL数据库,我可以’t找到一种访问它的反应方式: - /
[…要公共API,例如Android Lib Anko,彩票3d字谜 HTML Builder或新发布的Spring 5框架。正如我们将在即将到来的例子中看到,Ktor也使用此类DSL,该DSL能够启用[…]
[…]你喜欢了解更多关于春天及其梦幻般的彩票3d字谜支持的信息,我鼓励您阅读此博客文章作为[…]