java - 在 Java 中,Azure applicationinsights 启动程序依赖项与 Spring Boot 3.x.x 不兼容

标签 java spring-boot azure azure-application-insights appinsights

我想将应用程序洞察集成到我的 Spring Boot 应用程序**(v3.x.x)** 中,为此我在 build.gradle 中使用了以下依赖项。

实现“com.microsoft.azure:applicationinsights-spring-boot-starter:2.6.4”

在我的 Controller 中,我注入(inject)了 TelemetryClient bean,如下所示:

@RestController
@RequestMapping("/")
public class UserController {
    @Autowired
    TelemetryClient telemetryClient;

    @GetMapping("/greetings")
    public String greetings() {
        // send event
        telemetryClient.trackEvent("URI /greeting is triggered");

        return "Hello World!";
    }
}

并且,要连接到我使用的 Azure 应用程序见解:

APPLICATIONINSIGHTS_CONNECTION_STRING=somevalue

在 IntellijIdea 中运行应用程序时作为环境变量。

现在,应用程序运行并提示:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field telemetryClient in com.example.demo.app.insights.UserController required a bean of type 'com.microsoft.applicationinsights.TelemetryClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.microsoft.applicationinsights.TelemetryClient' in your configuration.

但是,一旦我将 Spring Boot 版本从 3.x.x 降级到 2.7.11,它就可以使用同一段代码正常工作并启动应用程序。

还有其他人在 3.x.x 上也遇到过与 Spring Boot 类似的 appinsights 问题吗?或者知道最新的 Spring Boot 版本中的修复情况吗?

最佳答案

此问题已在 https://github.com/microsoft/ApplicationInsights-Java/issues/3016 中得到解决

以下依赖项与 Spring Boot 3.x 兼容:

api('com.microsoft.azure:applicationinsights-core:3.4.11');
// api 'com.microsoft.azure:applicationinsights-spring-boot-starter:2.6.4' // not compatible with spring 3.x

之后可以像这样注入(inject) TelemetryClient Bean:

@RestController
@RequestMapping("/")
public class UserController {

    private final TelemetryClient telemetryClient = new TelemetryClient();

    @GetMapping("/greetings")
    public String greetings() {
        // send event
        telemetryClient.trackEvent("URI /greeting is triggered");

        return "Hello World!";
    }
}

关于java - 在 Java 中,Azure applicationinsights 启动程序依赖项与 Spring Boot 3.x.x 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76073120/

相关文章:

tomcat - 在 spring REST 应用程序中引用来自 tomcat 的依赖 jar

java - 关闭 Spring Boot AWS 自动配置

azure - 在 Azure 上读取 RabbitMQ 消息

java - 将默认浏览器作为字符串返回的方法?

java - Java Web 应用程序中的异步任务

java - 获取在 try 方法中设置的 Java bean

azure - SQL Azure联合拆分设计和查询

java - 如何覆盖特定的 Seam 组件?

java - 如何使用 Spring Data JPA 保存具有手动分配标识符的实体?

python - Azure函数应用程序: Microsoft. Azure.WebJobs.EventHubs : Value cannot be null.(参数 'receiverConnectionString')