SoulChill19
SoulChill19

Reputation: 1

NoSuchFieldError: Companion for azure.application-insights

Currently migrating my spring-boot app to use the latest compatibile version for JDK 8. I'm currently receiving this error when running the spring-boot-app

ightsTelemetryAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'azure.application-insights-com.microsoft.applicationinsights.boot.ApplicationInsightsProperties': Initialization
 of bean failed; nested exception is java.lang.NoSuchFieldError: Companion
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:165)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:175)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:155)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:97)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:174)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5218)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:727)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:695)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1016)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1903)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:750)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webRequestTrackingFilterRegistrationBean' defined in class path resource [com/microsoft/applicationinsights/boot/ApplicationInsightsWebMvcAutoConfiguration.class]: Unsatisfied dependency expressed through method
'webRequestTrackingFilterRegistrationBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.microsoft.applicationinsights.boot.ApplicationInsightsTelemetryAutoConfiguration': Unsatisfied dependency expressed through constructor paramet
er 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'azure.application-insights-com.microsoft.applicationinsights.boot.ApplicationInsightsProperties': Initialization of bean failed; nested exception is java.lang.NoSuchFieldError: Companion
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:541)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)

I have these versions for my applicationinsights, spring-boot-starter-parent 2.7.12, spring-cloud-dependencies 2021.0.3 and spring-cloud-starter-bootstrap in my parent pom. I updated to the latest version of applicationinsights but I'm still receiving the same error.

           <dependency>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>applicationinsights-spring-boot-starter</artifactId>
                <version>2.6.4</version>
            </dependency>
            <dependency>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>applicationinsights-logging-logback</artifactId>
                <version>2.6.4</version>
            </dependency>

I've updated the following artifacts and I've also added spring-cloud-starter-bootstrap dependency

artifact previous version updated version
spring-boot-starter-parent 2.2.4.RELEASE 2.7.12
spring-cloud-dependencies Finchley.M9 2021.0.3
applicationinsights-spring-boot-starter 1.0.0-BETA 2.6.4
application-insights-logging 2.1.1 2.6.4

Upvotes: 0

Views: 199

Answers (1)

Pravallika KV
Pravallika KV

Reputation: 8694

The error seems to be occurring due to the initialization of the azure.application-insights-com.microsoft.applicationinsights.boot.ApplicationInsightsProperties bean (or) due to the version incompatibility between Azure Application Insights package and Spring Boot you are using.

Try upgrading/ downgrading the Azure Application Insights library version which is compatible with your Spring Boot version.

(Or)

create your application with Spring Boot 2.7.x version to use ApplicationInsights package 2.6.4 version.

Run mvn clean install once you upgrade the versions.

enter image description here

Alternatives:

  • AFAIK,applicationinsights-spring-boot-starter package is for old way of configuring Application Insights.
  • Instead you can use Java Agent/ applicationinsights-runtime-attach in your application to configure AppInsights.
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-runtime-attach</artifactId>
    <version>3.4.14</version>
</dependency>

pom.xml:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>applicationinsights-runtime-attach</artifactId>
            <version>3.4.14</version>
        </dependency>
    </dependencies>

Create applicationinsights-dev.json under src>main>resources folder and add :

{
"connectionString":"InstrumentationKey=XXXXX;IngestionEndpoint=https://XXXXXX.in.applicationinsights.azure.com/;LiveEndpoint=https://XXXXX.livediagnostics.monitor.azure.com/"
}

Add below configuration in applications.properties:

-Dapplicationinsights:
runtime-attach:
configuration:
classpath:
file: "applicationinsights-dev.json"

main()

package  com.javabycode.springboot;
import  com.microsoft.applicationinsights.attach.ApplicationInsights;
import  org.springframework.boot.SpringApplication;

@SpringBootApplication
public  class  MyWebApplication{

public  static  void  main(String[] args) throws  Exception {

ApplicationInsights.attach();

SpringApplication.run(MyWebApplication.class, args);

}

}

References:

Upgrading from 2.x - Azure Monitor Application Insights Java - Azure Monitor | Microsoft Learn Configure Azure Monitor Application Insights for Spring Boot - Azure Monitor | Microsoft Learn

Upvotes: 0

Related Questions