Spring boot 如何注入依赖项CqlSession?

Spring boot 如何注入依赖项CqlSession?,spring-boot,kotlin,dependency-injection,cassandra,shedlock,Spring Boot,Kotlin,Dependency Injection,Cassandra,Shedlock,为了使用Cassandra的ShedLock,我需要CqlSession来创建一个CassandraLockProvider,根据 但是,我总是遇到以下错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.datastax.oss.driver.api.core.CqlSession' available: expected at least 1 b

为了使用Cassandra的
ShedLock
,我需要
CqlSession
来创建一个
CassandraLockProvider
,根据

但是,我总是遇到以下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.datastax.oss.driver.api.core.CqlSession' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
下面的完整示例再现了该问题:

src/main/kotlin/com/foo/cqlsessioninjection/application.kt
settings.gradle
src/test/kotlin/com/foo/cqlsessioninjection/integration/ContextTest.kt
src/test/resources/application.yml
src/test/resources/cql/cql_session_injection.cql

您引用的文档指出,必须创建
CqlSession
bean,但它并没有真正解释您是如何做到这一点的。我将把它作为一个文档问题报告给这个项目,使它更加明确


您需要SpringBoot2.3才能获得CassandraV4支持。您正在使用的版本(2.2x)使用Cassandra v3和一个完全不同的API。

您引用的文档指出必须创建一个
CqlSession
bean,但它并没有真正解释您是如何做到这一点的。我将把它作为一个文档问题报告给这个项目,使它更加明确


您需要SpringBoot2.3才能获得CassandraV4支持。您正在使用的版本(2.2x)使用Cassandra v3和完全不同的API。

谢谢,我已经打开了。你能详细谈谈你对卡桑德拉v3和v4的看法吗?SpringBoot2.3目前仍处于发行候选状态。这是使其正常工作的先决条件?驱动程序有多个主要版本-3.x和4.x-它们有显著的体系结构差异,并且不兼容二进制文件。3.x使用集群/会话对象,4.x使用CqlSession。Spring使用特定的驱动程序版本,如2.2使用3.x,RC版本使用4.xThanks作为解释。我尝试使用版本
2.3.0.M4
(并添加
maven{url'https://repo.spring.io/milestone' }
,但错误仍然存在。让我们看看
ShedLock
维护人员是否找到了解决方案。是的,它没有在M4中实现。搜索问题或查看发行说明是正确的。如果您希望为您自动配置东西,您需要
2.3.0.RC1
,非常感谢。这解决了问题。这是完整信息(工作)示例:谢谢,我已经打开了。您能详细说明一下您对Cassandra v3和v4的评论吗?Spring Boot 2.3目前仍处于发行候选状态。这是使其正常工作的先决条件?驱动程序有多个主要版本-3.x和4.x-它们具有显著的架构差异,并且不兼容二进制。3.x使用集群/会话对象,4.x使用CqlSession。Spring使用特定的驱动程序版本,如2.2使用3.x,RC版本使用4.xThanks作为解释。我尝试使用版本
2.3.0.M4
(并添加
maven{url'https://repo.spring.io/milestone' }
,但错误仍然存在。让我们看看
ShedLock
维护人员是否找到了解决方案。是的,它没有在M4中实现。搜索问题或查看发行说明是正确的。如果您希望为您自动配置东西,您需要
2.3.0.RC1
,非常感谢。这解决了问题。这是完整信息(工作)示例:
package com.foo.cqlsessioninjection


import com.datastax.oss.driver.api.core.CqlSession
import net.javacrumbs.shedlock.core.LockProvider
import net.javacrumbs.shedlock.provider.cassandra.CassandraLockProvider
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component


@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT23H")
class CqlSessionInjection


@Configuration
class SchedulerConfiguration {
    @Bean
    fun lockProvider(cqlSession: CqlSession): LockProvider {
        return CassandraLockProvider(cqlSession)
    }
}

@Component
class SomeTask {
    @Scheduled(cron = "*/1 * * * * ?") // every second
    @SchedulerLock(name = "some_task")
    fun runSomeTask() {
        println("some task")
    }
}

fun main(args: Array<String>) {
    runApplication<CqlSessionInjection>(*args)
}

buildscript {
    ext {
        kotlinVersion = '1.3.71'
        springBootVersion = '2.2.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.foo'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'net.javacrumbs.shedlock', name: 'shedlock-spring', version: '4.9.1'
    implementation group: 'net.javacrumbs.shedlock', name: 'shedlock-provider-cassandra', version: '4.9.1'

    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-cassandra'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8'
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect'
    implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.3.3'

    compile group: 'com.google.guava', name: 'guava', version: '23.3-jre'

    testCompile group: 'org.cassandraunit', name: 'cassandra-unit-spring', version: '3.11.2.0'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
    annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
}

task downloadDependencies(type: Exec) {
    configurations.testRuntime.files
    commandLine 'echo', 'Downloaded all dependencies'
}

compileKotlin.dependsOn(processResources)
compileJava.dependsOn(processResources)

rootProject.name = 'CqlSessionInejction'

package com.foo.cqlsessioninjection.integration

import org.cassandraunit.spring.CassandraDataSet
import org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener
import org.cassandraunit.spring.EmbeddedCassandra
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestExecutionListeners
import org.springframework.test.context.junit4.SpringRunner

@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestExecutionListeners(
    listeners = [CassandraUnitDependencyInjectionTestExecutionListener::class],
    mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
@CassandraDataSet(value = ["cql/cql_session_injection.cql"], keyspace = "shedlock")
@EmbeddedCassandra
class ContextTest {
    @Test
    fun `test task`() {
        println("starting to sleep")
        Thread.sleep(4000)
        println("done sleeping")
    }
}

spring:
  data:
    cassandra:
      contact-points: localhost
      port: 9142
      keyspace_name: shedlock
DROP KEYSPACE IF EXISTS shedlock;

CREATE KEYSPACE shedlock
WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1};

CREATE TABLE shedlock.lock (
    name text PRIMARY KEY,
    lockUntil timestamp,
    lockedAt timestamp,
    lockedBy text
);