程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了WebFlux 和 Kotlin 在多模块应用程序中使用 ReactiveCrudRepository大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决WebFlux 和 Kotlin 在多模块应用程序中使用 ReactiveCrudRepository?

开发过程中遇到WebFlux 和 Kotlin 在多模块应用程序中使用 ReactiveCrudRepository的问题如何解决?下面主要结合日常开发的经验,给出你关于WebFlux 和 Kotlin 在多模块应用程序中使用 ReactiveCrudRepository的解决方法建议,希望对你解决WebFlux 和 Kotlin 在多模块应用程序中使用 ReactiveCrudRepository有所启发或帮助;

我在 Kotlin 和 WebFlux 中有一个示例 SpringBoot 应用程序。我将整个应用程序划分为模块(就像我在 asp.net 中习惯的那样)。

模块:

  • 随处引用的核心(模型、DTO、助手等...)
  • 仅在业务中引用的数据(存储库、表...)
  • API 中引用的业务(服务、处理程序...)
  • API 实际 SpringBoot 应用程序

我现在的问题是如何正确使用 ReactiveCrudRepository 和一般的存储库。我在数据模块中有配置类来启用 R2dbcRepositorIEs。

@Configuration
@EnableR2dbcRepositorIEs("me.janam.data")
open class RepositorIEsConfiguration {
}

现在如果我创建表和存储库

interface IPersonRepository: ReactiveCrudRepository<Persontable,Long> {
    @query("SELECT * FROM person.person limit 1")
    fun getone(): Mono<Persontable>
}

并尝试在业务模块中使用它,但出现错误

CAnnot access 'org.springframework.data.repository.reactive.ReactiveCrudRepository' which is a supertype of 'me.janam.data.features.person.repositorIEs.IPersonRepository'. check your module classpath for missing or conflicTing dependencIEs

当然如果我添加

implementation("org.springframework.data:spring-data-commons:2.4.6")

进入我的业务模块一切正常。但不知何故,这让我觉得很奇怪。这是正确的方法吗?

也不是我的主要问题的一部分,但这里是完整的配置,我喜欢听到一些关于它的意见,因为我主要是 asp.net 开发人员。谢谢。

root - setTings.gradle.kts:

rootProject.name = "springboot-example"
include(":API")
include(":business")
include(":data")
include(":core")

root - gradle.propertIEs:

kotlin.code.style=official
kotlin_version=1.4.31
kotlinx_coroutInes_reactor_version=1.4.3

r2dbc_postgresql_version=0.8.7.RELEASE
postgresql_version=42.2.19

spring_context_version=5.3.5

root - build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.4.31"
    ID("io.spring.dependency-management") version "1.0.11.RELEASE"
}

subprojects {
    apply(plugin = "io.spring.dependency-management" )

    dependencymanagement {
        
    }
}

group = "me.janam"
version = "1.0-SNAPSHOT"

repositorIEs {
    mavenCentral()
}

dependencIEs {
    //implementation("org.springframework:spring-context:5.3.5")

    testImplementation(kotlin("test-junit5"))
    testImplementation("org.junit.jupiter:junit-jupiter-API:5.6.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-ENGIne:5.6.0")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile>() {
    kotlinoptions.jvmTarget = "13"
}

核心 - build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm")
}

group = "me.janam"
version = "1.0-SNAPSHOT"

repositorIEs {
    mavenCentral()
}

dependencIEs {
    testImplementation(kotlin("test-junit5"))
    testImplementation("org.junit.jupiter:junit-jupiter-API:5.6.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-ENGIne:5.6.0")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile>() {
    kotlinoptions.jvmTarget = "13"
}

数据 - build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val kotlinx_coroutInes_reactor_version: String by project
val r2dbc_postgresql_version: String by project
val postgresql_version: String by project
val spring_context_version: String by project

plugins {
    kotlin("jvm")
}

group = "me.janam"
version = "1.0-SNAPSHOT"

repositorIEs {
    mavenCentral()
}

dependencIEs {
    implementation(project(":core"))

    implementation("org.springframework:spring-context:$spring_context_version")

    implementation("org.springframework.boot:spring-boot-starter-data-r2dbc:2.4.4")
    runtimeOnly("io.r2dbc:r2dbc-POSTGResql:$r2dbc_postgresql_version")
    runtimeOnly("org.POSTGResql:POSTGResql:$postgresql_version")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutInes-reactor:$kotlinx_coroutInes_reactor_version")

    testImplementation(kotlin("test-junit5"))
    testImplementation("org.junit.jupiter:junit-jupiter-API:5.6.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-ENGIne:5.6.0")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile>() {
    kotlinoptions.jvmTarget = "13"
}

业务 - build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val kotlinx_coroutInes_reactor_version: String by project
val spring_context_version: String by project

plugins {
    kotlin("jvm")
}

group = "me.janam"
version = "1.0-SNAPSHOT"

repositorIEs {
    mavenCentral()
}

dependencIEs {
    implementation(project(":core"))
    implementation(project(":data"))

    implementation("org.springframework:spring-context:$spring_context_version")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutInes-reactor:$kotlinx_coroutInes_reactor_version")

    implementation("org.springframework.data:spring-data-commons:2.4.6") //Todo

    testImplementation(kotlin("test-junit5"))
    testImplementation("org.junit.jupiter:junit-jupiter-API:5.6.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-ENGIne:5.6.0")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile>() {
    kotlinoptions.jvmTarget = "13"
}

API - build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    ID("org.springframework.boot") version "2.4.4"
    // Přesunuto do rootu
    ID("io.spring.dependency-management")
    kotlin("jvm")
    kotlin("plugin.spring") version "1.4.31"
}

group = "me.janam"
version = "1.0-SNAPSHOT"
//java.sourceCompatibility = JavaVersion.VERSION_13

repositorIEs {
    mavenCentral()
}

dependencIEs {
    implementation(project(":core"))
    implementation(project(":business"))

    //implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")
    implementation("org.springframework.boot:spring-boot-starter-rsocket")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutInes-reactor")
    //runtimeOnly("io.r2dbc:r2dbc-POSTGResql")
    //runtimeOnly("org.POSTGResql:POSTGResql")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
}

tasks.withType<KotlinCompile> {
    kotlinoptions {
        freeCompilerArgs = listof("-XJsr305=Strict")
        jvmTarget = "13"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

解决方法

向每个子项目添加相同的依赖集可能会让人感到奇怪,但这完全没问题。为了在给定的子项目中使用给定的依赖项,您必须将其指定为该子项目的依赖项。

然而,有比实际将 import 语句复制粘贴到每个构建文件更简洁的方法来实现这一点。我建议在您的根 subprojects 中指定一个 build.gradle.kts 部分并将共享和公共依赖项放在那里:

subprojects {
    dependencies {
        // Put items here that are used in all/most subprojects:
        implementation("org.springframework:spring-context:$spring_context_version")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutInes-reactor:$kotlinx_coroutInes_reactor_version")

        // You can put this import here or just keep it in the build files
        // for subprojects where it will actually get used
        implementation("org.springframework.data:spring-data-commons:2.4.6")

        testImplementation(kotlin("test-junit5"))
        testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
        testRuntimeOnly("org.junit.jupiter:junit-jupiter-ENGIne:5.6.0")
    }
}

您仍然需要在每个子构建文件中使用 implementation(project(":core")) 类型语句指定每个子项目所依赖的其他子项目,但上面的 dependencies 块负责使指定的库可以全部访问子项目。

大佬总结

以上是大佬教程为你收集整理的WebFlux 和 Kotlin 在多模块应用程序中使用 ReactiveCrudRepository全部内容,希望文章能够帮你解决WebFlux 和 Kotlin 在多模块应用程序中使用 ReactiveCrudRepository所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: