简体   繁体   English

无法实例化接口 org.springframework.context.ApplicationContextInitializer (Spring 5.2.3 + Spring Boot 2.2.3)

[英]Cannot instantiate interface org.springframework.context.ApplicationContextInitializer (Spring 5.2.3 + Spring Boot 2.2.3)

I'm using Spring 5.2.3.RELEASE and Spring Boot 2.2.3.RELEASE .我正在使用 Spring 5.2.3.RELEASE和 Spring Boot 2.2.3.RELEASE

When trying to run some integration tests for my application, I'm getting the following error:尝试为我的应用程序运行一些集成测试时,出现以下错误:

 Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
 Caused by: java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer : org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:445)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:427)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:420)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:272)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:253)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.acme.service.MyService.main(MyService.java:60)
    ... 8 more
 Caused by: java.lang.NoSuchMethodError: org.springframework.core.KotlinDetector.isKotlinReflectPresent()Z
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:187)
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:441)

I believe that this error relates to incompatible versions of some components.我认为此错误与某些组件的版本不兼容有关。

This similar issue https://github.com/spring-projects/spring-boot/issues/15970 says:这个类似的问题https://github.com/spring-projects/spring-boot/issues/15970说:

isKotlinReflectPresent was added to Spring Framework in 5.1. isKotlinReflectPresent 在 5.1 中被添加到 Spring Framework 中。 Therefore, I would suspect that you have some jars from Framework 5.0 (or earlier) in the classpath.因此,我怀疑您的类路径中有一些来自 Framework 5.0(或更早版本)的 jar。

but I cannot determine which dependency is causing the problem?但我无法确定是哪个依赖项导致了问题?

Here is my pom.xml:这是我的 pom.xml:

 <dependencies>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.10.1</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.10.1</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.10.1</version>
      </dependency>
      <dependency>
        <groupId>com.github.joschi.jackson</groupId>
        <artifactId>jackson-datatype-threetenbp</artifactId>
        <version>2.6.4</version>
      </dependency>
      <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
      </dependency>
      <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.196</version>
      </dependency>
      <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>2.7.5</version>
      </dependency>
      <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>okhttp</artifactId>
        <version>2.7.5</version>
      </dependency>
      <dependency>
        <groupId>com.squareup.okio</groupId>
        <artifactId>okio</artifactId>
        <version>1.6.0</version>
      </dependency>
      <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.6.0</version>
      </dependency>
      <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.0.8</version>
      </dependency>
      <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
        <version>2.0.2</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.9</version>
      </dependency>
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.13</version>
      </dependency>
      <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>9.0.30</version>
      </dependency>
      <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.1.4</version>
      </dependency>
      <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest</artifactId>
        <version>2.1</version>
      </dependency>
      <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.9</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.30</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>2.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <version>2.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test-autoconfigure</artifactId>
        <version>2.2.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.threeten</groupId>
        <artifactId>threetenbp</artifactId>
        <version>1.3.1</version>
      </dependency>
      <dependency>
        <groupId>io.gsonfire</groupId>
        <artifactId>gson-fire</artifactId>
        <version>1.8.3</version>
      </dependency>
    </dependencies>

And below is the result from mvn dependency:tree .下面是mvn dependency:tree的结果。 Any help is much appreciated.任何帮助深表感谢。 Thanks!谢谢!

[INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ my-service ---
[INFO] com.acmecorp.service:my-service:jar:1.0.0-SNAPSHOT
[INFO] +- com.github.acmecorpapi:swagger-ui-annotation:jar:1.0.0-SNAPSHOT:compile
[INFO] |  +- org.springframework:spring-core:jar:5.0.8.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.0.8.RELEASE:compile
[INFO] |  \- org.webjars.npm:swagger-ui-dist:jar:3.20.6:runtime
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.10.1:compile
[INFO] +- org.springframework:spring-web:jar:5.2.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot:jar:2.2.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.3.RELEASE:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.2.3.RELEASE:runtime
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.3.RELEASE:runtime
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:runtime
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.12.1:runtime
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.30:runtime
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:runtime
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.25:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.2.3.RELEASE:runtime
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.10.2:runtime
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.10.2:runtime
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.10.2:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.3.RELEASE:runtime
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.30:runtime
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.30:runtime
[INFO] |  \- org.springframework.boot:spring-boot-starter-validation:jar:2.2.3.RELEASE:runtime
[INFO] |     \- org.hibernate.validator:hibernate-validator:jar:6.0.18.Final:runtime
[INFO] |        +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:runtime
[INFO] |        \- com.fasterxml:classmate:jar:1.3.4:runtime
[INFO] +- org.apache.commons:commons-lang3:jar:3.9:compile
[INFO] +- io.swagger:swagger-annotations:jar:1.6.0:compile
[INFO] +- com.github.acmecorp.logging:acmecorp-logging-logback:jar:1.0.0-36:runtime
[INFO] |  +- ch.qos.logback:logback-classic:jar:1.2.3:runtime
[INFO] |  +- ch.qos.logback:logback-core:jar:1.2.3:runtime
[INFO] |  +- com.github.acmecorp.util:util-process-identifier:jar:1.16.0-118:runtime
[INFO] |  \- org.apache.commons:commons-text:jar:1.2:runtime
[INFO] +- com.github.joschi.jackson:jackson-datatype-threetenbp:jar:2.6.4:compile
[INFO] +- org.postgresql:postgresql:jar:42.2.9:runtime
[INFO] +- com.h2database:h2:jar:1.4.196:runtime
[INFO] +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.30:compile
[INFO] |  \- org.apache.tomcat:tomcat-annotations-api:jar:9.0.30:compile
[INFO] +- jakarta.validation:jakarta.validation-api:jar:2.0.2:compile
[INFO] +- org.springframework:spring-beans:jar:5.2.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.2.3.RELEASE:runtime
[INFO] |  +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.2.3.RELEASE:runtime
[INFO] |  |  \- org.springframework.boot:spring-boot-actuator:jar:2.2.3.RELEASE:runtime
[INFO] |  \- io.micrometer:micrometer-core:jar:1.3.2:runtime
[INFO] |     +- org.hdrhistogram:HdrHistogram:jar:2.1.11:runtime
[INFO] |     \- org.latencyutils:LatencyUtils:jar:2.0.3:runtime
[INFO] +- org.springframework:spring-context:jar:5.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.2.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:5.2.3.RELEASE:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.10.1:compile
[INFO] +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.2.3.RELEASE:runtime
[INFO] |  +- com.zaxxer:HikariCP:jar:3.4.2:runtime
[INFO] |  \- org.springframework:spring-jdbc:jar:5.2.3.RELEASE:runtime
[INFO] |     \- org.springframework:spring-tx:jar:5.2.3.RELEASE:runtime
[INFO] +- org.springframework:spring-webmvc:jar:5.2.3.RELEASE:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.10.1:compile
[INFO] +- org.threeten:threetenbp:jar:1.3.1:compile
[INFO] +- org.flywaydb:flyway-core:jar:5.1.4:runtime
[INFO] +- com.acmecorp.service:my-service-contract:jar:1.0.0-SNAPSHOT:runtime
[INFO] +- junit:junit:jar:4.12:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest:jar:2.1:test
[INFO] +- org.springframework:spring-test:jar:5.2.3.RELEASE:test
[INFO] \- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.2.3.RELEASE:test
[INFO]    \- org.springframework.boot:spring-boot-test:jar:2.2.3.RELEASE:test

MyService.java我的服务

@AcmeCorpSwaggerUI("com.acme.service.my-service.contract") // Custom interface
@SpringBootApplication(
    scanBasePackages = {"io.swagger", "com.example.service"})
public class MyService implements WebMvcConfigurer
{

    @Value("${https.port}")
    private int httpsPort;

    @Value("${management.server.port}")
    private int adminPort;

    public static void main(String[] args)
    {
        SpringApplication.run(MyService.class, args);
    }
    ...
}

You've since edited out your pom, but I suspect that it's due to you explicitly defining versions for dependencies that Spring Boot manages.您已经编辑了您的 pom,但我怀疑这是由于您明确定义了 Spring Boot 管理的依赖项的版本。

Your pom should have Spring Boot as its parent:你的 pom 应该有 Spring Boot 作为它的父级:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId> <!-- No version defined -->
    </dependency>
...

I'd suggest grooming your pom by:我建议通过以下方式修饰你的 pom:

  • Remove versions of dependencies that Spring Boot manages.删除 Spring Boot 管理的依赖项版本。
  • Remove dependencies that are already brought in transitively by Spring Boot starters.删除已经由 Spring Boot starters 引入的依赖项。

This dependency, shown in mvn dependency:tree above, was bringing in an older version of Spring, resulting in the runtime error:上面的mvn dependency:tree显示的这种依赖引入了旧版本的 Spring,导致运行时错误:

[INFO] +- com.github.acmecorpapi:swagger-ui-annotation:jar:1.0.0-SNAPSHOT:compile
[INFO] |  +- org.springframework:spring-core:jar:5.0.8.RELEASE:compile

Options to resolve this are to:解决此问题的选项是:

  • Update the above dependency to use Spring 5.2.3更新上面的依赖以使用 Spring 5.2.3
  • Use Spring 5.0.8 in my project在我的项目中使用 Spring 5.0.8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring-Boot-Maven-Plugin,无法实例化接口 org.springframework.context.ApplicationContextInitializer - Spring-Boot-Maven-Plugin, Cannot instantiate interface org.springframework.context.ApplicationContextInitializer java.lang.IllegalArgumentException:无法实例化接口org.springframework.context.ApplicationContextInitializer - java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer 线程“主”java.lang.IllegalArgumentException 中的异常:无法实例化接口 org.springframework.context.ApplicationContextInitializer - Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer 运行 spring boot 应用程序错误:无法实例化接口 org.springframework.context.ApplicationListener - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener 运行 spring 启动应用程序错误:eclipse intellij 无法实例化接口 org.springframework.context.ApplicationListener - Run spring boot application error: eclipse intellij Cannot instantiate interface org.springframework.context.ApplicationListener 构建错误:无法实例化接口org.springframework.boot.SpringApplicationRunListener Spring Boot - build error: Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener Spring Boot 由 Spring Boot 中的 :org.springframework.context.NoSuchMessageException 引用 - Coused by :org.springframework.context.NoSuchMessageException in Spring Boot Spring-找不到org / springframework / context / event / EventListenerFactory的类文件 - Spring - Cannot find class file for org/springframework/context/event/EventListenerFactory 线程“main”中的异常 java.lang.IllegalArgumentException:无法实例化接口 org.springframework.context.ApplicationListener - Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener 发布 Spring Boot 3 更新 - 无法实例化工厂 class [org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver] - Post Spring Boot 3 update - Unable to instantiate factory class [org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM