我想将应用程序洞察集成到我的 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/