将 Sentry 与 Spring Boot 2.6.x 而不是 2.5.x 一起使用时如何修复无法解析的循环引用?

How to fix unresolvable circular reference when using Sentry with Spring Boot 2.6.x instead of 2.5.x?

使用 org.springframework.boot 版本 2.5.9,认为工作正常,但使用 2.6.02.6.12.6.22.6.3),我出现以下错误:

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sentryOptions': Requested bean is currently in creation: Is there an unresolvable circular reference?

(完整日志:https://gist.github.com/Dobiasd/be7810282a06b538ccee0078ab2267aa

这是我重现问题的最小示例:

Application.kt:

package com.acme.foo.bar

import io.sentry.spring.EnableSentry
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Configuration

@EnableSentry
@Configuration
class SentryConfiguration

@SpringBootApplication
class Application

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

IntegrationTest.kt:

package com.acme.foo.bar.integration

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.junit.jupiter.SpringExtension

@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class FullStackTest {
    @Test
    fun init_context() {
    }
}

build.gradle.kts

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

plugins {
    id("org.springframework.boot") version "2.6.3"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.6.10"
    kotlin("plugin.spring") version "1.6.10"
}

group = "com.acme"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation(group = "org.springframework.boot", name = "spring-boot-starter-web")
    implementation(group = "io.sentry", name = "sentry-spring", version = "5.5.3")

    testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-test")
    testImplementation(group = "org.jetbrains.kotlin", name = "kotlin-test-junit")
}

tasks {
    withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "11"
        }
    }

    withType<Test> {
        testLogging.exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
        useJUnitPlatform()
    }
}

知道我做错了什么吗?或者 sentry-spring 可以与 Spring Boot 2.6.x 一起使用(还)?

找到解决方案。而不是 io.sentry:sentry-spring 必须在依赖项中使用 io.sentry:sentry-spring-boot-starter 然后从代码中删除以下内容:

@EnableSentry
@Configuration
class SentryConfiguration

我刚刚在我的实际项目中测试过,记录的错误仍然正确地发送到Sentry。